Skip to content

Instantly share code, notes, and snippets.

@sebastianczech sebastianczech/docker.md
Last active Jul 23, 2019

Embed
What would you like to do?
Docker - notes from training

Links

Commands

Info about Docker

docker version
docker info
docker system info 

Hello world in Docker

docker run hello-world
docker images
docker ps -a

docker rm 7582a701a0c1
docker rmi hello-world

Pull image

docker image pull alpine
docker image ls

Simple WWW server

docker run -d --name www httpd:alpine
docker image ls
docker container ls -a

Inspect WWW server

docker container inspect www
curl http://172.17.0.2
docker stop www
docker start www

Show processes

docker top www
ps aux | grep httpd
docker exec -it www ps aux

Execute command in container

docker exec -it www /bin/bash

Show changed files in container's filesystem (not on volumes) and check log path

docker diff www
docker container inspect www | grep LogPath

Logs from container

docker logs www
docker logs www --help
docker logs www --follow
docker logs www --tail 3
docker logs www --tail 3 --follow

History and stats

docker history httpd:alpine
docker history alpine:latest

docker stats

Execute other command than default in container

docker run --name test -it httpd:alpine top
docker rm test

docker run --name test -it --rm httpd:alpine ls

Inspect image

docker image inspect httpd:alpine

Disk usage

docker system df
df -i

Build image from Dockerfile

mkdir ex1
cd ex1/
vi Dockerfile
docker image build -t training:mc2 .

Remove all containers

docker stop $(docker ps -a -q)
docker rm $(docker ps -a -q)

Remove unused images

docker image prune

Remove unused data

docker system prune

Show and delete volumes

docker volume ls
docker volume prune 

Show networks

docker network ls

Run containers with different networks

docker exec httpd ifconfig

docker run --rm --network host alpine ifconfig
docker run --rm --network none alpine ifconfig

docker run --rm alpine ifconfig
docker run --rm --network bridge alpine ifconfig

Publish exposed port from container into randomed port on host

docker run -d --name httpd --network bridge -P training:httpd

Port forwarding from container into host

docker run -d --name httpd --network bridge -p 80:80 training:httpd 
docker run -d --name httpd --network bridge -p 127.0.0.1:80:80 training:httpd 

netstat -nlt
brctl show

Create new network

docker network create wawa
docker network ls

docker run -d --name httpd --network wawa -p 80:80 training:httpd
docker run -d --name mongo --network wawa -v mongo-data:/data/db training:mongo 

docker network inspect wawa

docker rm -f httpd 
docker run -d --name httpd --network wawa -p 80:80 -e MONGO_CS="mongodb://mongo:27017" training:httpd

docker rm -f mongo
docker run -d --name mongo --network wawa -v mongo-data:/data/db training:mongo /usr/bin/mongod --bind_ip_all

Connect container with new network

docker network create orange
docker network connect orange httpd 
docker exec httpd ifconfig

Dockerfiles

Ubuntu with mc

Many layers

FROM ubuntu:latest
RUN apt-get update
RUN apt-get install -y mc
RUN apt-get clean
RUN rm -rf /var/lib/apt/lists/*

One layer

FROM ubuntu:latest
RUN apt-get update && \
    apt-get install -y mc && \
    apt-get clean && \
    rm -rf /var/lib/apt/lists/*
    
# Command to build: docker image build -t training:mc2 .

Web server

##
## Dockerfile for training:httpd, kamilbaran/nobleprog_training:httpd
## mkdir httpd
## cd httpd
## wget https://ds.kamilbaran.pl/training/html.zip
## unzip html.zip
## rm html.zip
## vi Dockerfile
## docker image build -t training:httpd .
## cd ..
##

FROM alpine:3.9

RUN apk add apache2 php7-apache2 php7-mongodb --update-cache --no-cache            

RUN ln -sf /dev/stdout /var/log/apache2/access.log && \
    ln -sf /dev/stderr /var/log/apache2/error.log && \
    mkdir -p /run/apache2/

EXPOSE 80
CMD ["/usr/sbin/httpd", "-D", "FOREGROUND"]

# https://ds.kamilbaran.pl/training/html.zip
COPY ["html/", "/var/www/localhost/htdocs/"]
ENV MONGO_CS="mongodb://localhost:27017"

#### start httpd container
## docker run -d --name httpd training:httpd
#### with disable network isolation:
## docker run -d --name httpd --network host training:httpd

Mongo database

##
## Dockerfile for training:mongo, kamilbaran/nobleprog_training:mongo
## mkdir mongo
## cd mongo
## vi Dockerfile
## docker image build -t training:mongo .
## cd ..
##

FROM alpine:3.9

RUN mkdir -p /data/db && \
    addgroup -S -g 27017 mongodb && \
    adduser -S -u 27017 -g mongodb mongodb && \
    chown -R mongodb:mongodb /data

RUN apk add mongodb --update-cache --no-cache
 
VOLUME ["/data/db/"]
EXPOSE 27017
USER mongodb
CMD ["/usr/bin/mongod", "--dbpath=/data/db"]

#### start Mongo:
## docker run -d --name mongo --network host training:mongo
#### start with defined name of volume:
## docker run -d --name mongo --network host -v mongo-data:/data/db training:mongo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.