In this post we going to learn how to run docker images, and we going to use some options and commands most used.
- Docker installed, you can check my post about install Docker here.
To run docker images you can use the command
docker container run
docker container run -it -d --name my_container image_name command
docker container run -it -d --name ubuntu_dev ubuntu /bin/bash
OK lets explain all the options we have here.
- The -i option means that it will be interactive mode (Keep STDIN open even not attached)*
- The -t option gives you STDOUT (Allocate a pseudo-TTY)*
- The -d option (daemon mode) keeps the container running in the background*
- –name my_container name of the container*
- image_name docker will try to find the image locally at first if does not exist it will search for the image in docker hub.
- /bin/bash is the command it runs as pid 1, if this command stop if the container stops*.
* Not required.
If you want to know more about STDIN and STDOUT you can read here
Docker Hub is an image repository where you can find official and unofficial docker images.
1. Put the last command to docker download and run the latest ubuntu image from docker hub
$ docker container run -it -d --name dev_ubuntu ubuntu /bin/bash Unable to find image 'ubuntu:latest' locally latest: Pulling from library/ubuntu 33c9bf8777b5: Pull complete 3a1900e16211: Pull complete d53c8a81c254: Pull complete Digest: sha256:cbcf86d7781dbb3a6aa2bcea25403f6b0b443e20b9959165cf52d2cc9608e4b9 Status: Downloaded newer image for ubuntu:latest 91773f80517d8c9f7777db9ea651e48f993d9eb66bf6b0e5bf6240845308c05e
2. Verify that the container is running with
docker container ls
$ docker container ls CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 91773f80517d ubuntu "/bin/bash" 2 minutes ago Up 2 minutes dev_ubuntu
3. Now lets connect to the container with
docker container exec
docker container exec -it dev_ubuntu /bin/bash
4. Once you connect to the container you can update, install packages or configure your container, once you want to return to the host you can type
Well now that you know how to run containers you can look into Docker Hub for more containers, you will find almost any application for all purpose, like samba, apache, redis, nginx, dns, dhcp, etc
Now, lets clean up
5. Stop the container
docker container stop dev_ubuntu
If you want to start the container later you can use the command "docker container start container_name"
6. If you don't going to use the container again, you can remove the container after stopping it, this will delete all data except the image.
docker container rm dev_ubuntu
7. And if you dont going to use the image anymore you can delete it
docker image rm ubuntu:latest
Well done, now you know how to run container with Docker and use some basic commands