Docker, my first container
You will learn here to create and manage your first docker container.
This is a practical tutorial. You can find the theoretical part here: Docker, what is it?.
Let's take an example
As an example, let's install the web server Nginx.
Our goal is to display "my first container with docker" when navigating to http://localhost:1337 using your usual browser.
First, you need to install docker: https://docs.docker.com/get-docker/.
For our example, here is the basic structure we will use:
- The folder nginx-docker contains the resources to build the docker container.
- The folder project contains the HTML file available for the web browser.
- The Dockerfile contains the description of our image.
cd ~ mkdir nginx-docker project echo "my first container with docker" > project/index.html touch nginx-docker/Dockerfile
Build a Docker image
Docker hub already proposes an official image for nginx. Let's use it in the Dockerfile!
This small Dockerfile is all you need for our example!
Next step, we need to build our docker image based on this Dockerfile. To make things clearer, you can give it a name.
docker build -t my_nginx_image ~/nginx-docker
Docker executes line by line the content of the Dockerfile. On the "FROM" instruction, it automatically downloads
the basic image if it's missing. You can run the command a second time if you want to check that the download
process is only executed the first time.
docker build takes a folder as argument and not directly the Dockerfile
because the folder can contain other files and be used as a building "context".
You currently have 2 images: nginx 1.18 and my_nginx_image. You can see the images with:
docker image ls
Create a Docker container
Guess what? It's time to create your first container \o/.
Summary of the needs:
- Accessing localhost:1337 must display index.html. Nginx is, by default, listening on the port 80: we need to bind the port 1337 of the host to the port 80 of the container.
- It would be awesome to be able to change the content of index.html without having to restart the container: we need a "volume" linking the project "folder" of our host to the root folder used by nginx inside the container "/usr/share/nginx/html". This kind of detail is written on the nginx page of the docker hub: read it carefully.
docker run -p 1337:80 -v ~/project:/usr/share/nginx/html my_nginx_image # -p : port redirection # -v : creation of a volume. You must use absolute paths.
At http://localhost:1337 you can see "my first container with docker".
Congratulations, your first container is running!
You can see the running containers with:
docker rundid not give you back the prompt and continues to run upfront. The option
-dturns on the detached mode.
The container has a weird name like nervous_chaplygin. Fun fact, a random name is generated each time you
docker runwithout explicitly providing a name. You can find the algo here. The option
--namegives a name to a container. The name can be used with most of the docker commands in the same way as the container ID.
docker runseveral times, without naming it, you will each time create a new container.
For the next commands we will use as an example the container ID 61363317039f.
You can stop the container with:
docker stop 61363317039f
The container is not active and is not displayed anymore by
However, the container still appears with
docker ps --all, proving
that it still lives and still consumes your machine's resources.
There is several ways of deleting a stopped container:
docker container rm 61363317039f # it's an alias of docker container rm docker rm 61363317039f # remove all stopped containers docker container prune
In this tutorial you learned to build an image, to create a container from this image then to delete it.
Continue your apprenticeship of docker in the tutorial Docker, basic commands.
Have a nice day :)