Containers How To: Difference between revisions

From PUBLIC-WIKI
Jump to navigation Jump to search
Eyales (talk | contribs)
No edit summary
Eyales (talk | contribs)
No edit summary
Line 99: Line 99:
* '''ENV variable_name=variable_data'''
* '''ENV variable_name=variable_data'''
: Purpose: Creates an environment variable inside a docker container
: Purpose: Creates an environment variable inside a docker container
Reference:
: https://linuxize.com/post/how-to-build-docker-images-with-dockerfile/


== Common Docker commands for using Docker hub registry ==
== Common Docker commands for using Docker hub registry ==

Revision as of 05:09, 12 June 2019

How to install Docker (Windows Platform)

  • Login to the machine using privileged account
  • Download the latest Docker build from:
https://download.docker.com/win/edge/Docker%20Desktop%20Installer.exe

How to install Docker (Ubuntu)

  • Login to the machine using privileged account
  • Follow the instructions below:
https://docs.docker.com/install/linux/docker-ce/ubuntu/

How to install Docker (RHEL / CentOS)

  • Login to the machine using privileged account
  • Follow the instructions below:
https://docs.docker.com/install/linux/docker-ce/centos/

Common Docker commands

  • Check the Docker version:
docker --version
  • Pull and run “hello-world” image from Docker hub:
docker run hello-world
  • List images:
docker images
  • List all images:
docker image ls
  • Pull an image from Docker hub:
  • docker image pull alpine
  • Remove Docker image (by image name) from the local machine:
docker image rm nginx
  • List all containers:
docker container ls --all
  • List active containers:
docker ps
  • List all container processes (same as docker container ls command)
docker ps --all
  • Pull nginx web server from Docker hub and run it on port 80 on the local machine:
docker run --detach --publish 80:80 --name webserver nginx
Note: Open browser and enter the URL http://localhost to make sure the web server is working
  • Start a container in the background:
docker run -d jenkins
  • Start an interactive shell command from a container (by image name):
docker run -it nginx bash
  • Mount a folder from the local machine into a docker container, and map port 8080 locally to port 80 on the container (container name nginx):
docker run -v /full/path/to/html/directory:/usr/share/nginx/html:ro -p 8080:80 -d nginx
  • Stop running container (by container name) from the local machine:
docker container stop webserver
  • Start (a stopped) docker container (by container name) on the local machine:
docker container start webserver
  • Kill all running containers:
docker kill $(docker ps -q)
  • Remove all stopped containers:
docker rm $(docker ps -a -q)
  • Remove container (by container name) from the local machine:
docker container rm webserver
  • View logs from a running container (by container name):
docker container logs webserver
  • Show live logs of running container:
docker logs -f c7337
  • View top processes inside a container (by container name):
docker container top webserver
  • Show exposed ports of a container:
docker port c7337

Building a Docker image

  • Create a sample Dockerfile on the local machine, with the following content:
FROM nginx
COPY html /usr/share/nginx/html
  • Run the command below (from the local folder containing Dockerfile and html folder) to build a custom docker image:
docker build -t mynginx .
  • Build a Docker image from a Dockerfile:
docker build -f path/to/Dockerfile -t mynginx .
  • Force rebuild of Docker image (from the current directory):
docker build --no-cache .
  • Convert a container to image:
docker commit c7337 myimage
  • Run the newly created custom docker image:
docker run --name mynginx -d -p 8080:80 mynginx

Docker file instructions

  • FROM <base_image_name>:<image_version>
Purpose: Set the base image to create the container from
Example: FROM Ubuntu:15.04
  • RUN <command>
Purpose: Execute commands inside the container
Example: RUN apt-get update -y
  • CMD [“command”,”parameter1”,”parameter2”]
Purpose: Define a default command to run when the container starts
Example: CMD [“echo”,”Hello World!”]
  • EXPOSE <port>
Purpose: Informs docker that the container listens on specific port number at runtime
Example: EXPOSE 80/tcp
  • COPY <src> <dest>
Purpose: Copy files/folders from the local machine into the Docker container
Example: COPY html /www/html #Copy html folder into /www/html inside the container
  • ADD <src_file> <dest>
Purpose: Copy archive file from the local machine into the docker container and extract it, or copy the content of a folder inside a URL into the docker container
Example 1: ADD myfile.tar.gz /myfolder
Example 2: ADD http://www.example.com/folder /folder
  • ENV variable_name=variable_data
Purpose: Creates an environment variable inside a docker container

Reference:

https://linuxize.com/post/how-to-build-docker-images-with-dockerfile/

Common Docker commands for using Docker hub registry

  • Login to the docker hub using registered account:
docker login -u <username> -p <password>
  • Tag a docker image created locally:
docker tag <image-id> <username>/<repo-name>:<tag>
Example: docker tag 3fd9065eaf02 shrikantlavhate/kerneltalks
  • Push a docker image from the local machine into the docker hub:
docker push <username>/<repo-name>:<tag>
Example: docker push shrikantlavhate/kerneltalks
  • Pull a docker image from docker hub:
docker pull <username>/<repo-name>:<tag>
Example: docker pull alpine:latest
  • Logoff docker hub:
docker logout