Docker: Using the Command Line to Create Containers
Friends, to get started with Docker and use the command line, you need to use the command docker create
. With this command, you can easily create a new container without having to run it immediately. This functionality allows you to configure the container settings before execution.
For example, imagine you want to create a container for the web server Nginx. This task is quite simple! You just need to add a few options to the docker create
command and then you can run the container with the docker start
command.
We will see a practical example of how to use this command below. Additionally, we have prepared line-by-line explanations for you to clarify things.
Keep in mind that for all these tasks, you must have Docker installed on your system and the Docker service should be running. Now let's head to the code!
docker create --name my-nginx -p 8080:80 nginx
Line-by-line Code Explanation
In this example, we will analyze the command below:
docker create --name my-nginx -p 8080:80 nginx
docker create: This part is the main command for creating a container that Docker uses to create a new container.
--name my-nginx: This option specifies the name of the container. You can assign any name you prefer.
-p 8080:80: This section configures the ports. Port 8080 on your machine will connect to port 80 in the container. Thus, you will be able to access the Nginx website through port 8080.
nginx: Finally, this is the image name that you want to create the container based on. Here, we are using the Nginx image.