How to Use docker

choubertsprojects

VPN offers!

1. NordVPN

2. Surfshark

3. ExpressVPN

Docker is a software platform that helps developers and system administrators manage apps in Linux containers. Containers are isolated from one another, making it easier to deploy and run applications with less overhead. In addition to being efficient, containers can also have the same or similar environments as production systems so they’re easy to debug when something goes wrong while testing locally.

Docker is an open-source software container technology that can be used to run any application in a self-contained environment. It has been gaining popularity in recent years and is now being used by some of the most popular tech companies like Facebook, Google, Microsoft, and Amazon. In this tutorial, you will learn how to use docker with github.

How to Use docker

Environment variables are your greatest friend if you need to declare several configuration settings. Docker and, more especially, Docker Compose, like many other technologies, can create and read environment variables to keep your setups tidy and modular. This article will show you how to create various containers, environments, and more using docker-compose environment variables.

Prerequisites

If you want to follow along, make sure you have the following items:

  • A Linux server with administrative rights. Ubuntu 18.04.5 LTS is used in this lesson.
  • On the Linux host, Docker is installed. Docker v19.03.11 is used in this lesson.

Installation and Use of Docker on Ubuntu (In the Real World)

Using the Docker Compose File to Declare Environment Variables

Containers might need a great deal of setup. And not every container setup will be unique. Some settings, such as a MySQL database credential, may be shared across many containers. You must manually save the credentials in each container, and you must update the credentials many times if the credentials change.

Variables in the environment may help alleviate the problem. Those shared parameters should be saved as environment variables! Instead of repeating yourself in the containers, refer to the environment variables. Only one setting has to be updated when the credentials change: the environment variable.

To begin, create an environment variable and save it in the Docker Compose file. The procedures following will use environment variables to save settings for a fictional MySQL database.

1. On your local computer, open a terminal.

2. Create a folder called /docker-compose-demo, then shift (cd) your working directory to that folder. All of the files you’ll create in this tutorial will be in the /docker-compose-demo folder.

cd /docker-compose-demo mkdir /docker-compose-demo mkdir /docker-compose-demo

3. Open your preferred text editor and paste the code from the excerpt below into it. Save the file to the /docker-compose-demo directory as docker-compose.yml. The settings for your application’s services are stored in docker-compose.yml.

In the code excerpt below:

  • Docker Compose uses the mysql:5.7 base image to construct a new container.
  • The Docker Compose file has three environment variables: MYSQL ROOT PASSWORD, MYSQL ALLOW EMPTY PASSWORD, and MYSQL RANDOM ROOT PASSWORD. The values of each environment variable are stated. After the: symbol, the environment variable receives its value.
  • The mysql service will build the mysql container.

Version of the Docker compose file: “2.2” services: # Defining the mysql service: # Defining the base image that will be used: mysql:5.7 container name: mysql hostname: mysql # Identifying the environmental variable: # ENVIRONMET VARIABLE NAME: “value of an environmental variable” “root password” is MYSQL ROOT PASSWORD. MYSQL ALLOW EMPTY PASSWORD: “password” MYSQL ALLOW EMPTY PASSWORD: MYSQL RANDOM ROOT PASSWORD: “password” MYSQL RANDOM ROOT PASSWORD:

To function effectively, certain containers depend on environment variables. One of these containers is MySQL, as seen in the sample. The container will give an error if you don’t define the environment variables it expects. The mistake is shown below.

If the environment variables are missing, MySQL will give an error. If the environment variables are missing, MySQL will give an error.

4. Run the docker-compose up command after that. The docker-compose up command builds the container by reading the YAML file (docker-compose.yml) written in the previous stage. All of the services specified in the Docker Compose file are started by the docker-compose up command.

5. Now, use the docker exec and env commands to see whether all three environment variables are present in the container.

You may log into the container by using the docker exec command. When you run env, it will produce a list of the current environment variables along with their values.

# ee8af8bfcd41 /bin/bash is the container docker exec -it ee8af8bfcd41

The environment variables' values are pulled straight from the docker-compose.yml file you created previously.The environment variables’ values are pulled straight from the docker-compose.yml file you created previously.

Environment Variables Substituting

You learnt how to specify environment variables directly inside the Docker Compose file by hard coding them in the previous section. If you need to protect your credentials, this strategy isn’t ideal. Alternatively, keep the settings for environment variables in a.env file that only the administrator may access.

Let’s try substituting variables for the environment. In your computer, type:

1. In the same /docker-compose-demo directory, create a file named.env and paste the code below into it. The file will include all of the environment variables as well as their values.

# Setting environmental variables’ values MYSQL ROOT PASSWORD=”root password” MYSQL ALLOW EMPTY PASSWORD=”password” MYSQL RANDOM ROOT PASSWORD=”password”

2. Using the setfacl command, change the permissions of the.env file you generated in the previous step. The following command grants read/write rights to the.env file to the root user.

  • The m option enables you to establish file/folder permissions.
  • u is the user (root) who will be given access to the file /*docker-compose-demo/.env.

/docker-compose-demo/.env setfacl -m “u:root:rw”

3. With your preferred editor, open the docker-compose.yml file and comment out the environment section from the previous part. In the Compose file, add the following code to establish default values for the environment variables.

Docker sets the settings via the command line or files, such as the.env file in this case. Docker replaces the values as needed in either scenario.

# Using hardcoded values in variables environment to define the environmental variable: ### A static method MYSQL ROOT PASSWORD: “root password” ### # MYSQL ROOT PASSWORD: “root pass MYSQL ALLOW EMPTY PASSWORD: “password” # MYSQL ALLOW EMPTY PASSWORD: ### Substitution ### MYSQL RANDOM ROOT PASSWORD: “password” MYSQL ROOT PASSWORD: ${MYSQL ROOT PASSWORD} $MYSQL ALLOW EMPTY PASSWORD MYSQL ALLOW EMPTY PASSWORD MYSQL ALLOW EMPTY PASSWORD $MYSQL RANDOM ROOT PASSWORD MYSQL RANDOM ROOT PASSWORD MYSQL RANDOM ROOT PASSWORD

The variables are defined in the code using the string interpolation approach. String interpolation variables are defined as $variable. At runtime, Docker sets the values of the environment variables.

4. Run the docker-compose up command once again. The docker-compose.yml file searches the.env file for the values of the environment variables when you execute the docker-compose up command. The docker-compose command searches the project directory or the parent folder of your compose file for a.env file.

Docker-compose replaces the values for the environment variables defined in docker-compose.yml in the.env file and starts the service as soon as it finds them. The container described in the docker-compose.yml file is created when the service is started.

Using Files with Multiple Environment Variables for Multiple Environments

You’ve studied two methods for declaring environment variables so far. First, specify environment variables in the docker-compose.yml file. The second method involves defining all of the environment variables in a single.env file and then substituting them.

When working in a single context, both techniques are appropriate. You’ll need a different solution if you have numerous settings, such as Production and/or Testing. Let’s see how to create multiple.env files with varied names to meet your different environments!

The following sample will simulate the typical IT Ops environments: development, quality assurance, and production. Returning to your terminal:

1. Create a.env.dev file in your /docker-compose-demo directory. Save the file after copying/pasting the text of the sample code below. This file contains the database settings for your MySQL development environment.

# Environmental Variables (.env.dev) file MYSQL ROOT PASSWORD=”password DEV” MYSQL ALLOW EMPTY PASSWORD=”password1″ MYSQL RANDOM ROOT PASSWORD=”password1″

2. Create a.env.qa file in the same directory and save the contents of the sample code below in it. These are the MySQL quality assurance environment database configurations.

# Environmental Variables (.env.qa) file MYSQL ROOT PASSWORD=”password QA” MYSQL ALLOW EMPTY PASSWORD=”password2″ MYSQL RANDOM ROOT PASSWORD=”password2″

3. Create a.env.prod file to contain the MySQL production environment database settings. The following are the contents:

# Environmental Variables.env.prod file MYSQL ROOT PASSWORD=”password PROD” MYSQL ALLOW EMPTY PASSWORD=”password3″ MYSQL RANDOM ROOT PASSWORD=”password3″

4. Next, use the —env-file option to provide the.env.dev file in the docker-compose command. The docker-compose command searches the current /docker-compose-demo directory for the.env.dev file.

—env-file.env.dev up docker-compose

By passing the file as a parameter, you may save it wherever and label it appropriately.

Use Docker Stop Containers Without Breaking Anything!

You may simply choose alternative environment files and deploy them by replacing the.env.dev file with either the.env.qa or the.env.prod file, as seen below.

# Using the QA file in the docker-compose command docker-compose —env-file.env.qa up # Running docker-compose —env-file.env.prod up with the Prod file

5. Run the docker exec command to see whether the configuration file (.env.dev) was properly read. You may log into the container by using the docker exec command. The command env prints a list of current environment variables.

The container contains all three environment variables: MYSQL ROOT PASSWORD, MYSQL ALLOW EMPTY PASSWORD, and MYSQL RANDOM ROOT PASSWORD. Their values are taken from the env.dev file.

Using the docker exec command and the.env.dev file to log into the containerUsing the docker exec command and the.env.dev file to log into the container

The env file is now included in the Docker Compose File.

You learned how to define environment variables in.env files in the previous part. You end up with a lot of lines and references in the docker-compose.yml when you keep environment variable values in the.env file separately.

Consider include the env file in your docker-compose.yml file to decrease the amount of references and lines for environment variables in your docker-compose.yml file.

Let’s look at how to use the env file in a Docker compose file. In the terminal, once more:

1. Create a file called var.env to store the MySQL database settings. Add the following to the var.env file and save it in the same /docker-compose-demo directory.

MYSQL ROOT PASSWORD=”password NEW” MYSQL ALLOW EMPTY PASSWORD=”password NEW” MYSQL RANDOM ROOT PASSWORD=”password NEW”

2. Use your chosen editor to open the previously produced docker-compose.yml file. Replace the environment section in docker-compose.yml with env file, then add the location to the file with -./var.env.

Docker now searches in the same /docker-compose-demo directory for the./var.env file.

version: “2.2” mysql svc: services mysql:5.7 image container name: mysql hostname: mysql # Using env file to replace the environment: # # MYSQL ROOT PASSWORD: $MYSQL ROOT PASSWORD environment # MYSQL ALLOW EMPTY PASSWORD: $MYSQL ALLOW EMPTY PASSWORD env file:./var.env

You’ll see that all three settings and references have vanished. There is just one reference remaining. In the example, it may not seem to be much. However, in the actual world, the amount of allusions may quickly become overwhelming.

3. Run the docker-compose command next. The command searches the var.env file for values of environment variables and constructs the container you specified in the docker-compose.yml file.

The service is started by running docker-compose. The service is started by running docker-compose.

Docker constructs the services, which means it looked in the var.env file for the values for the environment variables. Run the docker exec and env commands one final time to be sure. In the container, you’ll see the values of all the environment variables (MYSQL ROOT PASSWORD, MYSQL ALLOW EMPTY PASSWORD, and MYSQL RANDOM ROOT PASSWORD).

Conclusion

You learnt how to declare environment variables using Docker compose in this lesson. The post demonstrated how Docker gives you a lot of options when it comes to specifying environment variables, whether you code them directly or use different files.

So, when using Docker Compose to run your Docker containers, which technique will you choose next?

Docker is a new technology that allows for the creation and execution of containers. These containers can be used to build, run and deploy applications. This article will show you how to use docker on your desktop computer. Reference: how to use docker desktop.

Related Tags

  • how to use docker compose
  • how to use docker in windows
  • install docker
  • docker hub
  • docker build