Skip to content

Instantly share code, notes, and snippets.

@roshanlabh
Created January 25, 2022 14:02
Show Gist options
  • Save roshanlabh/29439d1061b56ebb6325c68959c40fc4 to your computer and use it in GitHub Desktop.
Save roshanlabh/29439d1061b56ebb6325c68959c40fc4 to your computer and use it in GitHub Desktop.
Docker help

Docker self help guide

Docker is lightweight virtual machines.

  • To install docker on ubuntu
wget -qO- https://get.docker.com/ | sh
  • To run hello-world image
sudo docker run hello-world
  • To avoid typing sudo in front every time. Add docker user to current user group
sudo usermod -aG docker <user>

# Now, you can run
docker run hello-world
  • For any docker command help, use
docker help

Also, run docker COMMAND --help for more information on a command.

  • To know docker version
docker --version
  • To know client/server docker version, use below command
docker version
  • To know downloaded docker image on system, use
docker images
docker image ls
  • To search an image on Docker image
docker search centos
  • To pull any docker image from Docker Hub
docker pull <image-name>:<tag>
docker pull httpd

If no tag is provided then by default latest tag is used

  • To run docker image (By running docker image, it converts image into container then)
docker run hello-world

If above image is not on local (or means it is not previously downloaded/pulled) then docker daemon automatically download it from Docker Hub.

  • To run docker image in background, use -d
docker run -d ubuntu bash
docker run -d centos:7 ping 127.0.0.1 -c 100
  • In above command, centos 7 will start running in background and "ping 127.0.0.1 -c 100" command will be fired in centos. But as it is running in background mode. So we will not be able to see ping output. So to see output of the containers running in background, we use below command.
docker logs <container-ID>
docker logs -f <container-ID>
  • To run docker image’s shell, use -it
docker run -it ubuntu bash

It will return ubuntu terminal means you will in ubuntu terminal where you can run any ubuntu command

docker run -d -P tomcat:7

This will run tomcat in background as well as attach to any random port of Host system. Please note Host OS is the os on which you are running docker. For example I am running my docker on my ubuntu system. So to see, on which port tomcat is running. Use "ps -a" command.

docker ps -a

It will give you result of all container which is running and stopped.

  • To create your own image, create Dockerfile first under your project directory like below.
FROM httpd:2.4
COPY ./ /usr/local/apache2/htdocs/

then run docker build -t <new-image-name> . command.

docker build -t sample-html-app .

It will create image from your Dockerfile. Then to run this image, use docker run -dit --name <new-container-name> -p <port-number>:80 <above-image-name>

docker run -dit --name jumboapp -p 8080:80 my-apache2
  • To start any stopped container, use
docker container start <container-ID>
  • To stop any running docker container, use
docker container stop <container-ID>
  • To Kill docker container, use
docker kill <container-ID>
docker kill $(docker ps -q)
  • To remove all containers
docker rm $(docker ps -aq)
  • We can even copy files from the container back into our system
docker cp demo:/usr/src/demo-server/file.txt file.txt

look at file.txt locally

docker cp -a <container-name>:/usr/local/apache2/htdocs/ ./
docker login
docker tag <image-name> username/<image-name>
docker push username/<image-name>
  • Execute command inside running container. For examples
docker exec -it <running-container-name> /bin/bash
docker exec trusting_jang cat /etc/hosts
  • Docker compose cheatsheet
# Note: you need to cd first to where your docker-compose.yml file stored.
# Start containers in the background: 
docker-compose up -d

# Start containers on the foreground:
docker-compose up
# You will see a stream of logs for every container running.

# Stop containers:
docker-compose stop

# Kill containers:
docker-compose kill

# View container logs:
docker-compose logs

# Execute command inside of container:
docker-compose exec SERVICE_NAME COMMAND
# where COMMAND is whatever you want to run. Examples: * Shell into the PHP container,
docker-compose exec php-fpm bash

* Run symfony console,
docker-compose exec php-fpm bin/console

* Open a mysql shell,
docker-compose exec mysql mysql -uroot -pCHOSEN_ROOT_PASSWORD
  • Create a new image from a container’s changes
docker commit <running container-ID or container-name> roshanlabh/ms-interview:latest
docker push roshanlabh/ms-interview:latest
  • Filter Containers by Image name
docker ps -qa --filter ancestor=ubuntu
  • Delete Containers by Image name
docker rm $(docker ps -qa --filter ancestor=ubuntu)
  • One common issue while using docker on Ubuntu comes is that, when you run ubuntu container and try to 'apt-get update', the update gets failed by giving follwoing error
	“Err:1 http://security.ubuntu.com/ubuntu bionic-security InRelease        
	  Temporary failure resolving 'security.ubuntu.com'
	Err:2 http://archive.ubuntu.com/ubuntu bionic InRelease                  
	  Temporary failure resolving 'archive.ubuntu.com'
	Err:3 http://archive.ubuntu.com/ubuntu bionic-updates InRelease
	  Temporary failure resolving 'archive.ubuntu.com'
	Err:4 http://archive.ubuntu.com/ubuntu bionic-backports InRelease
	  Temporary failure resolving 'archive.ubuntu.com'
	Reading package lists... Done
	Building dependency tree       
	Reading state information... Done
	5 packages can be upgraded. Run 'apt list --upgradable' to see them.
	W: Failed to fetch http://archive.ubuntu.com/ubuntu/dists/bionic/InRelease  Temporary failure resolving 'archive.ubuntu.com'
	W: Failed to fetch http://archive.ubuntu.com/ubuntu/dists/bionic-updates/InRelease  Temporary failure resolving 'archive.ubuntu.com'
	W: Failed to fetch http://archive.ubuntu.com/ubuntu/dists/bionic-backports/InRelease  Temporary failure resolving 'archive.ubuntu.com'
	W: Failed to fetch http://security.ubuntu.com/ubuntu/dists/bionic-security/InRelease  Temporary failure resolving 'security.ubuntu.com'
	W: Some index files failed to download. They have been ignored, or old ones used instead.”

So, the work around of this issue are following. Firstly, go inside the running ubuntu container

docker exec -it <container-ID> bash

check the /etc/resolve.conf file, what is there into it.

root@7b65610d2440:/etc# cat resolv.conf

If it shows (Google's Public DNS IPs) below IPs

nameserver 8.8.8.8
nameserver 8.8.4.4

Then you probably have to change these DNS with your host DNS. So now, run below command in your host (means outside the running ubuntu container bash)

nmcli dev show | grep 'IP4.DNS'

Then, it may show you something like below

IP4.DNS[1]:                             192.168.0.1
IP4.DNS[2]:                             8.8.8.8
IP4.DNS[3]:                             8.8.4.4
IP4.DNS[1]:                             192.168.0.1

So now, just change your ubuntu container DNS (those Google DNS under ubuntu container) by above host DNS (192.168.0.1)

root@7b65610d2440:/etc# mv resolv.conf resolv.orig
root@7b65610d2440:/etc# echo "nameserver 192.168.0.1" > resolv.conf

It's done. Now just run the 'apt-get update' again. It will successfully able to run now.

Dockerfile examples

# Example 1
FROM httpd:2.4
COPY ./ /usr/local/apache2/htdocs/

# Example 2
FROM php:5.6-apache
RUN docker-php-ext-install mysqli
RUN docker-php-ext-install pdo pdo_mysql
COPY ./ /var/www/html/

# Example 3
FROM php:5.6-apache
RUN apt-get update; \
	apt-get install openssl libssl-dev libcurl4-openssl-dev -y; \
	pecl install mongodb; \
echo "extension=mongodb.so" > /usr/local/etc/php/conf.d/mongo.ini
COPY ./ /var/www/html/

# Example 4
FROM php:5.6-apache
# RUN docker-php-ext-install mongo
COPY ./php.ini /usr/local/etc/php/
COPY ./ /var/www/html/
command: --noauth

environment:
    MONGO_INITDB_ROOT_USERNAME: rlabh
    MONGO_INITDB_ROOT_PASSWORD: rlabh123
    MONGO_INITDB_DATABASE: slimapp
docker rmi unifydemoapp_gui:latest unifydemoapp_users:latest unifydemoapp_interview:latest unifydemoapp_db:latest

nmcli dev show | grep 'IP4.DNS'

mongorestore --db wattb-test dump/wattb-test

docker run -it --name wattb_ubuntu_1 -p 8080:80 -p 3000:3000 ubuntu
$ docker --version

$ docker-compose --version

$ docker run hello-world

$ docker run -it ubuntu bash

$ docker run -it python:3.9

Instead of python prompt. Run bash prompt so that we can run bash commands like pip install
$ docker run -it --entrypoint=bash python=3.9

Example: Dockerfile

FROM python:3.9

RUN pip install panadas

ENTRYPOINT ["bash"]

now run following command in cli

$ docker build -t test:pandas .

$ docker run -it test:pandas

Some good references:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment