Docker Commands

Docker Commands

  • docker version - shows docker version

  • docker run name_of_the_image - starts a container

  • docker run name_of_the_image:name_of_the_tag - starts specific version of a container

  • docker run name_of_the_container sleep number_of_seconds - container sleeps after n second

  • docker ps - shows a list of running containers

  • docker ps -a - shows list of all containers (stopped & running)

  • docker stop name_of_the_container - stops a container

  • docker rm name_of_the_container - removes a container

  • docker images - shows list of images

  • docker rmi name_of_the_image - removes an image

  • docker pull name_of_the_image - downloads an image

  • docker exec name_of_the_container cat /etc/hosts - executes a command

  • docker attach name_or_id_of_the_container - runs a container in attach mode

  • docker run -d name_of_the_container - runs a container in detach mode

  • docker run -it name_of_the_container - runs a container with interactive and terminal mode

  • docker run -p port_number_of_local_host : docker_container_port name_of_the_container - maps port_number_of_local_host to docker_container_port

  • docker run -v directory/outside/the/container : directory/inside/the/container name_of_the_container - mounts the directory/outside/the/container to directory/inside/the/container

  • docker inspect name_of_the_container - returns details in a JSON format

  • docker logs name_or_id_of_the_container - returns logs of the container


Docker Images

Before creating the docker image we have to create a docker file call it Dockerfile. After creating the docker file we will create an image with the docker command.

Dockerfile

Docker file is a text file that consists of two parts instructions and arguments.


FROM Ubuntu     // Start from a base operating system or another image 

RUN agt-get update      // Install all dependencies 
RUN agt-get install python      // Install all dependencies 

RUN pip install flask     // Install all dependencies 
RUN pip install flask-mysql     // Install all dependencies 

COPY . /opt/source-code     // Copy source code

ENTRYPOINT FLASK_APP=/opt/source-code/app.py flask run      // Specify Entrypoint

Docker file works in layers. If any layer fails then the docker will reuse the previous layers from the cache and start from when it failed.

Creating & Pushing Docker Image Command

To create an image locally we will use the following command:


docker build Dockerfile -t name/imageName

And to push the image to Docker Registry, we will use the following command:


docker push accountName/imageName