preface

“It works on my machine, why not yours?”

You may often hear this conversation between programmers 😂~~~

“Wuhu ~~~ I will help you!” Docker🐳 popped up and said.

“Sure enough, I can run on my machine.”

“Holding the grass, I used to build the environment for a day, I did it in 5 minutes.”

From the above conversation, we can see the value of Docker. The emergence of Docker not only solves the problem caused by the inconsistency of our operating environment, but also helps us easily share our working container, so that others can easily create a container with the same function through our shared container, saving the time we spend on environment construction 🚀. Since Docker is so good, let’s start learning!

This article by creating a mysql container can execute initialization SQL script as an example, to help you learn the basic knowledge of Docker. The article from the environment building, common instructions analysis and then step by step to the actual combat exercise, and finally achieve our goal. The actual combat is divided into three parts, from creating containers based on basic images, to learning to use volume mount for data sharing to create containers, and then to generate their own images based on Dockerfile, through their own images to create containers. Finally, I showed you how to transfer an image to the DockerHub repository for others to use.

The following figure is a summary of common docker instructions

Plus, we need to know a little bit about Linux before we learn about Docker!

1. Environment construction

1. Install the VM

Common VMS include VMware Workstation and VirtualBox. In this tutorial, VMware Workstation is installed.

2. Install Linux

Download the Linux system image and use the image to install the system. This tutorial uses the Ubuntu image.

3. Configure the Ubuntu basic environment

  • Set the root password: Run the following command to set the root password.
sudo passwd
Copy the code
  • Check the system shell: Ubuntu uses Dash as the default shell. You can run the following command to check the system shell.
ls -l /bin/sh
Copy the code
  • Switching system shell:
sudo dpkg-reconfigure dash
Copy the code

Run the following command and enter the password of user root. Select No by using arrow keys and press Enter to complete the bash switch.

You can use the view system shell command mentioned above to see if the shell is switched to bash.

4. Install the Docker

  • Install curl dependency
sudo apt-get install curl
Copy the code
  • Install the Docker:
curl -fsSL https://get.docker.com | bash -s docker --mirror Aliyun
Copy the code

2. The mirror

Image: Like a read-only template, as a programmer you can think of an Image as a class.

1. Search for mirrors:Docker Search Specifies the image name

docker search imageName
Copy the code

2. Pull or push an image:Docker pull/push image name

docker pull NAME:TAG
Copy the code

3. View the mirror:Docker images [image name]

The image name parameter is optional, indicating filtering by name.

Run the following command to view all images that have been downloaded.

docker images
Copy the code

4. Delete the mirror:Docker rmI [image name/image ID...

This command can delete multiple mirrors separated by Spaces.

Run the following command to delete all mirrors.

docker rmi -f $(docker image ls)
Copy the code

5. Create an image using Dockerfile:Docker build -t Image name -f Dockerfile path.

-f: specifies the path of the Dockerfile to be used. If this parameter is not used, the image is created using the Dockerfile in the current directory.

-t, -tag: indicates the name and label of the image. Usually, name is in the tag or name format. Multiple labels can be set for a single image in a single build;

Don’t forget the last. Oh ~ ~ ~

Docker build -t NAME:TAG -f Dockerfile path.Copy the code

6. Use containers to generate images:Docker commit Container ID Image name

docker commit containerId imageNAME
Copy the code

7. Rename the mirror:Docker tag Source image tag Target image tag

Tag a local image to place it in a repository. The new TAG will add one more record, but the IMAGE ID will not change, and they will still be the same IMAGE.

Docker tag Source mirror tag Destination mirror tag 'Copy the code

3. The container

Container: One or a group of applications that are created based on an image and run independently. Container can be regarded as the entity of the image runtime. Containers can be created, started, stopped, deleted, and so on.

1. Create and run the container:Docker run -itd --name Container name -p Host port: container port -v Host file path :/ Container file path -e Environment variable image name

-I: Runs the container in interactive mode, usually in conjunction with -t.

-t: reassigns a pseudo-input terminal to the container. It is usually used together with -i.

-d: Runs the container in the background and returns the container ID.

–name Container name: Specifies a name for the container.

-p Host port: Container port: specifies the port mapping.

-v Host file path :/ Container File path: Bind a volume. That is, bind a file or folder in a container to a file or folder on the host. The volumes are shared and synchronized.

-e Environment variable: Sets environment variables.

Image name: Based on which image is created.

Note: To create a container without running it, you can use the create command instead of the run command. The create command takes the same arguments as run and can then be started using the start command.

2. Start/Stop/restart:Docker start/stop/restart Container name/container ID

3. View all containers:docker ps -a

Run the preceding command to return the following information:

Details about the returned information are as follows:

CONTAINER ID: indicates the ID of a CONTAINER.

IMAGE: IMAGE used.

COMMAND: The COMMAND that is run when the container is started.

CREATED: time when the container was CREATED.

STATUS: indicates the STATUS of the container. There are seven statas: Created Restarting Running removing removing paused exited dead Note

PORTS: port information of the container and connection type used (TCP \ UDP).

NAMES: automatically assigned container NAMES.

4. View container logs:Docker logs -f --tail Number of rows Container name/container ID

-f: tracks real-time logs.

–tail Number of rows: How many rows are displayed from the end of the log. The default is all.

5. Delete the container:Docker RM Container name/container ID

Note: Stop the container before deleting it.

6. Enter the container:Docker exec -it container name/container id bash

You can add bash to the commands that create and start the container to go directly to the container after it is started.

7. Exit the container:exit

Use the exit command to exit from inside the container.

4.Dockerfile common commands

Comparison between RUN, CMD, and ENTRYPOINT

The RUN directive is the command that runs when the container is built; CMD and ENTRYPOINT run shell commands to start containers.

CMD is overwritten by the Docker run command, but ENTRYPOINT is not. In fact, any arguments specified by the Docker run command are passed as arguments again to the ENTRYPOINT directive. CMD and ENTRYPOINT can also be used together.

AND, COPY

The use of ADD and COPY commands is the same. The only difference is that ADD supports extracting and decompressing archive files (tar, gzip, bzip2, etc). Note that the COPY command must COPY directories in the same directory as the Dockerfile file

5. Use docker to start mysql

This tutorial demonstrates the process of creating and running mysql based on mysql:5.7 image. First, we input the command su and then the password of the root account to cut the user to the root account. In this way, we can avoid adding sudo before the docker command and make the following operations more convenient.

1. Build the basic version of mysql

First we generate an empty database based on the mysq:5.7 mirror.

Mysql: select * from mysql:5.7;

docker search mysql:5.7
Copy the code

To pull a mirror from mysql:5.7, run the following command:

Docker pull mysql: 5.7Copy the code

Mysql > create/run mysql-test mysql > create/run mysql-test mysql > create/run mysql-test mysql > create/run mysql-test mysql > create/run mysql-test

Docker run -itd --name mysql-test -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123456 mysql:5.7Copy the code

View container state: We can view the status of all containers by using the following command

docker ps -a
Copy the code

In the Status column we can see that the container has been running for two minutes.

We can also use the mysql connection tool to test whether the mysql application is working properly. Run the ifconfig command to query the IP address of the host, enter port 3306, account root, and password 123456 for the test.

2. Use the mounted volume to create and run a mysql database with initial data

Connect to mysql in the container, then create a database, create some tables and data in it, and export it as a SQL script. In this case, the exported script file is named test.sql.

Next, we need to think about how to create the container while running the initialization script.

We enter the container (mysql-test) with the following command:

docker exec -it mysql-test bash
Copy the code

Then enter ls -al to view the directories and files in the container

We found a /docker-entrypoint-initdb.d directory in the container, which can be used to execute initialization scripts in alphabetical order of filename. Therefore, we only need to mount a host directory to the container directory /docker-entrypoint-initdb.d, and then add our own initialization script to the mount host directory. /docker-entrypoint-initdb.d /docker-entrypoint-initdb.d /docker-entrypoint-initdb.d /docker-entrypoint-initdb.d /docker-entrypoint-initdb.d /docker-entrypoint-initdb.d /docker-entrypoint If mysql is started for the first time and the /var/lib/mysql content is empty because there is no data, the initialization script will be executed. Of course, since the /var/lib/mysql directory is typically mounted on the host machine, clearing this directory can also trigger initialization script execution.

Next we delete the container, create a mysql-test container with the same name, and start the container by adding the mount volume parameter.

First we exit the container with the exit command.

Mysql > stop mysql-test mysql > stop mysql-test mysql > stop mysql-test mysql

docker stop mysql-test
Copy the code

Mysql > delete mysql-test from mysql-test

docker rm mysql-test
Copy the code

Create /home/ mysql.init-data directory, and copy the test. SQL file to this directory. Prepare the host directory. Mysql > create mysql application root password 123456; port 3306; Mount /docker-entrypoint-initdb.d to /docker-entrypoint-initdb.d

docker run -itd --name mysql-test -p 3306:3306 -v /home/mysql/init-data/:/docker-entrypoint-initdb.d -e MYSQL_ROOT_PASSWORD = 123456 mysql: 5.7Copy the code

We enter the container (mysql-test) with the following command:

docker exec -it mysql-test bash
Copy the code

If the test. SQL file is found in the /docker-entrypoint-initdb.d directory, the file is successfully mounted.

Use the mysql connection tool to enter the mysql application and find that the tables and data in the database are initialized.

3. Repackage the image using Dockerfile to create and run a mysql database with initialization data

We have such a complex startup command, can we simplify the startup command, give some fixed things to the script to do?

Here I use the Dockerfile script to package a new image, and then create a mysql container based on the new image.

First, we switch to the /home directory by CD /home, create a Dockerfile directory in this directory, and create a Dockerfile file in the Dockerfile directory, and then write the following script in the file.

FROM mysql:5.7 ENV MYSQL_ROOT_PASSWORD=123456 COPY./init-data /docker-entrypoint-initdb.dCopy the code

The COPY command is used to host the files in the init-data folder of the Dockerfile directory on the machine. /docker-entrypoint-initdb.d), no volume mount, because Dockerfile script only supports anonymous mount.

Create a new image using Dockerfile (mysql:v1).

docker build -t mysql:v1 .
Copy the code

After the image is generated, you can run the following command to view the image status:

docker images
Copy the code

Mysql-test1 = mysql-test1; mysql:v1 = mysql-test1; mysql:v1 = mysql-test1;

docker run -itd --name mysql-test1 -p 3306:3306 mysql:v1
Copy the code

Mysql-test1 also initializes the mysql database.

6. Upload the image to dockerHub

Finally, we upload the mysql:v1 image to dockerHub. First we have to register an account on DockerHub, and then we log in using the following command.

docker login [OPTIONS] [SERVER]
Copy the code

-u: user name

P: password

Here we enter the command:

docker login
Copy the code

After you enter the user name and password, a message is displayed indicating that the login is successful. We then use the Docker push command to push the image to the repository. However, there are specifications for images pushed to DockerHub. The specifications are:

Docker push registered user name/image nameCopy the code

Mysql :v1 alter mysql:v1 alter mysql:v1 alter mysql:v1 alter mysql:v1

docker tag mysql:v1 zqhexor/mysql:v1
Copy the code

After the rename is complete, we can use the Docker push command to push the image to the DockerHub repository.

docker push zqhexor/mysql:v1
Copy the code

After successful push, we can see the image we uploaded in DockerHub.

End ~ 👻