Docker commands container run vs container exec command

Basic Docker Commands

Guide of Docker Commands with Examples
docker command container run vs container exec command

Docker :- is an open source tool developed to make it easier to create, deploy, and run applications throw containers, its all about speed, deployments, update faster, test faster, develop faster, recover faster.

Cotainers : are the fundamental building block of docker toolkit

Image vs Container :-

  • Image :- is the application that we want to run.
  • Container :- is an instance of that image running as a process and you can have many container running at the same image.

Dockers default Image 'registry' is called docker hub (for container images).

when you want an image doesn't exist at your device its installed from docker hub.

Docker Commands :

To check docker version:

  • sudo docker version

Return a lot of configuration and setup of our docker engine.

  • sudo docker info

To See docker commands

  • docker

  • sudo docker container run --publish 80:80 nginx

docker engine look for an image called nginx from docker hub.

started new container from nginx image.

opened port 80 on the host ip.

routes that traffic to the container ip, from port 80.

  • sudo docker container run --publish 80:80 --detach nginx
 --detach : is to tell docker to run it in background
  • docker container ls -to list all your containers (only show running container)
  • docker container stop (container id) -stop the container
  • docker container start (container id) -start the container

  • docker container run --publish 80:80 --detach --name web nginx
    --name web : add name for container. --detach : also can be written as -d

  • sudo docker container run --detach -p 3306:3306 --name db -e MYSQL_RANDOM_ROOT_PASSWORD=yes mysql
  • -p : stands for port.
  • -e : stands for environment

  • sudo docker container logs web -check logs for container name web.
  • docker container top web -check running process inside a container name web.
  • docker container —help -to see all container commands
  • docker container rm container-id -delete a container by its container id.
  • docker container rm -f container_id (-f stands for force), when you have a running container and you want to delete it.
  • sudo docker container start -ai container-name -to re-run an existing container.
  • sudo docker image ls -a - to see all docker images on your device.
  • sudo docker container inspect mysql -to see how this container configured when its started (data about container).

What does run docker do when we run a container ?

  1. look for that image locally in image cache.

  2. look in remote image. Ex: nginx image.

  3. download latest image by default
  4. creates new container based on that image
  5. give it a virtual ip on a private network inside docker.
  6. open up port 80 on host and forwards to port 80 in container.
  7. start container by using CMD in the image docker file, The CMD : specifies the instruction that is to be executed when a Docker container starts.

Container run vs Container exec

get a shell inside a container (run container interactively)

  • sudo docker container run -it

run additional second process on container

  • sudo docker container exec -it

  1. sudo docker container run -it --name web nginx bash :to starts new container interactively
  • to do things inside container live from command line : sh, bash …

  • you can change configuration file, install packages, any commands inside file system on container

  • to get out use : exit
  • after exit from command line, containers will stops because we change command running it into bash and we exit it, not the default command for the container image.

  1. sudo docker container exec :run additional command in existing container

  • run additional process in running container

  • if we want to see shell inside running container

  • used when you want to trouble shoot, change on a running container.

© 2020 PyOdoo.

TOPICS:  Docker Odoo