Docker – Quick start


Installation

The installation can change from an OS to another.

For more information, check this link.


Docker Commands

# Vérifier l'installation :
docker version

# Obtenir des informations sur la configuration :
docker info

# Liste des commandes possibles :
docker

# La structure d'une commande docker :
docker <command> <sub-command> (options)

Image vs. Container

Image : is the app we want to run (Check Docker Hub for the full list).
Container : is an instance of a specific image.


Docker containers management

Let’s start by running a container of nginx. Of course you are free to choose any image you want to start with.


Check the list here in DockerHub.

# Run our container
docker container run --publish 80:80 --detach nginx

Et voilà!

Docker by default will download the latest version of nginx from Docker Hub.
Then will run the container.
And finally, he will map all the 80 port traffic from our host to the port 80 of the container.
--detach or -d : is helpful if you want to run your container in background.

To know all the commands that it’s possible to run on a container you can use docker container help

docker container help

Usage:  docker container COMMAND

Manage containers

Commands:
  attach      Attach local standard input, output, and error streams to a running container
  commit      Create a new image from a container's changes
  cp          Copy files/folders between a container and the local filesystem
  create      Create a new container
  diff        Inspect changes to files or directories on a container's filesystem
  exec        Run a command in a running container
  export      Export a container's filesystem as a tar archive
  inspect     Display detailed information on one or more containers
  kill        Kill one or more running containers
  logs        Fetch the logs of a container
  ls          List containers
  pause       Pause all processes within one or more containers
  port        List port mappings or a specific mapping for the container
  prune       Remove all stopped containers
  rename      Rename a container
  restart     Restart one or more containers
  rm          Remove one or more containers
  run         Run a command in a new container
  start       Start one or more stopped containers
  stats       Display a live stream of container(s) resource usage statistics
  stop        Stop one or more running containers
  top         Display the running processes of a container
  unpause     Unpause all processes within one or more containers
  update      Update configuration of one or more containers
  wait        Block until one or more containers stop, then print their exit codes

Run 'docker container COMMAND --help' for more information on a command.

In order to list all process inside a container we can use top :

# List process inside our container
docker container top CONTAINER_NAME_OR_CONTAINER_ID

In addition to top, you can also use the following commands to get some informations :
docker container inspect ID : get the container configuration
docker container stats : get all containers performance stats

If we need to know the ID or the NAME of our container, we can use ls :

# List running containers
docker container ls

# List all created containers in our machine
docker container ls -a

As we saw before, run is used to run a container. So obviously, stop is used to stop it :

# Stop a container with id : CONTAINER_ID
docker container stop CONTAINER_ID

If you saw our previous run command, you will note that Docker gave a random name to our container.
But, of course you can customize this name by adding --name option to the run command.

# Give a name to our container : nginxServer
docker container run --publish 80:80 --detach --name nginxServer nginx

To delete a container? You guessed it! it’s the rm command.

# Delete a container by its ID
docker container rm CONTAINER_ID

# Forcer l'arrêt d'un conteneur en cours d'exécution
docker container rm -f CONTAINER_ID

# Remarque : On peut supprimer plusieurs conteneurs d'un seul coup
docker container rm CONTAINER_1_ID CONTAINER_2_ID CONTAINER_3_ID

One of the great things that Docker allows us to do, is to execute commands inside the container itself without installing SSH.


To do that, we got two options :
Option 1: docker container run -it (options) image COMMAND
t : allocate a pseudo-TTY
i : keep the session open, so you can run commands
COMMAND : if not specified, docker uses bash by default

Example: docker container run -it --name nginxServer nginx bash

Here we used bash command, so we get a terminal on the running container: root@eccd1ef7896c:/#


Option 2: docker container exec -it


About the author

nyounes

View all posts

Leave a Reply

Your email address will not be published. Required fields are marked *