Let’s learn docker by handson
Command | Description | Example |
---|---|---|
docker info | Display system-wide information | docker info |
docker version | Show the Docker version information | docker version |
docker pull | Pull an image or a repository from a registry | docker pull redis |
docker images | List images | docker images |
docker run | Run a command in a new container | docker run -d redis |
Command | Description | Example |
---|---|---|
docker ps | List containers | docker ps |
docker exec | Run a command in a running container | docker exec -it <id/name> sh |
docker inspect | Return low-level information on Docker objects | docker inspect <object id/name> |
docker logs | Fetch the logs of a container | docker logs <id/name> |
Command | Description | Example |
---|---|---|
docker stats | Display a live stream of container(s) resource usage statistics | docker stats |
docker network | Manage networks | docker network ls |
docker rename | Rename a container | docker rename <old> <new name> |
Command | Description | Example |
---|---|---|
docker stop | Stop one or more running containers | docker stop <id/name> |
docker rm | Remove one or more containers | docker rm <id/name> |
docker rmi | Remove one or more images | docker rmi <image id> |
Command | Description | Example |
---|---|---|
docker volume | Manage volumes | docker volume ls |
docker build | Build an image from a Dockerfile | dockr build . -t myapp:v1 |
docker [command] --help
> docker exec --help
Usage: docker exec [OPTIONS] CONTAINER COMMAND [ARG...]
Run a command in a running container
Options:
-d, --detach Detached mode: run command in the background
--detach-keys string Override the key sequence for detaching a container
-e, --env list Set environment variables
-i, --interactive Keep STDIN open even if not attached
--privileged Give extended privileges to the command
-t, --tty Allocate a pseudo-TTY
-u, --user string Username or UID (format: <name|uid>[:<group|gid>])
-w, --workdir string Working directory inside the container
https://docs.docker.com/engine/reference/commandline/docker/
Jenkins
docker run -p 8080:8080 -p 50000:50000 -v jenkins_home:/var/jenkins_home jenkins/jenkins:lts
This will store the workspace in /var/jenkins_home
All Jenkins data lives in there - including plugins and configuration
This will automatically create a ‘jenkins_home’ docker volume on the host machine, that will survive the container stop/restart/deletion
docker volume ls
Alpine Linux is a security-oriented, lightweight Linux distribution based on musl libc and busybox
Alpine image is only 5MB and has access to a package repository
Docker images today are big
Usually much larger than they need to be
There are a lot of ways to make them smaller, but the Docker populace still jumps to the ubuntu base image for most projects
The size savings over ubuntu and other bases are huge:
REPOSITORY TAG IMAGE ID CREATED SIZE
alpine latest 961769676411 4 weeks ago 5.58MB
ubuntu latest 2ca708c1c9cc 2 days ago 64.2MB
debian latest c2c03a296d23 9 days ago 114MB
centos latest 67fa590cfc1c 4 weeks ago 202MB
ruby 2.6.6 39853018958e 13 days ago 840MB
ruby 2.6.6-alpine d86341b50737 5 weeks ago 49.8MB
FROM ubuntu
RUN apt-get update
RUN apt-get install –y nginx
CMD [“echo”,”Image created”]
Command | Description |
---|---|
FROM | specify base image |
RUN | execute commands |
ENTRYPOINT | to configure a container that will run as an executable |
CMD | provide defaults for an executing container |
COPY | copies new files or directories |
Command | Description |
---|---|
ENV | sets the environment variable |
EXPOSE | informs Docker that the container listens on the specified network ports |
USER | sets the user |
WORKDIR | sets the working directory |
git clone https://github.com/slashpai/docker_k8s_training.git
cd docker_k8s_training
cd docker_apps
cd simple_webapp
docker run -d -p 9000:4567 –name simple-webapp1 simple-webapp:v1
docker run -d -p 9001:4567 –name simple-webapp1 –env APP_COLOR=yellow simple-webapp:v1
docker run -d -p 9002:4567 –name simple-webapp2 –env APP_COLOR=orange simple-webapp:v1
Go to http://localhost:9000, http://localhost:9001 and http://localhost:9002
FROM ruby:2.6.6-alpine
WORKDIR /usr/src/app
ENV APP_COLOR="pink"
COPY Gemfile ./
RUN gem install bundler && bundle install
COPY . .
EXPOSE 4567
CMD [ "ruby", "app.rb" ]
cd calculator
FROM ruby:2.6.6-alpine
WORKDIR /usr/src/app
COPY . .
ENTRYPOINT ["ruby", "calculator.rb"]
cd quiz_app
FROM node:15.3.0-alpine3.10
# Create app directory
WORKDIR /usr/src/app
# Install app dependencies
COPY package*.json ./
RUN npm install
# Bundle app source
COPY . .
EXPOSE 3000
CMD [ "node", "index.js" ]
https://hub.docker.com/search?q=&type=image&image_filter=store%2Cofficial
docker-compose <command>
build Build or rebuild services
config Validate and view the Compose file
create Create services
down Stop and remove containers, networks, images, and volumes
exec Execute a command in a running container
help Get help on a command
images List images
logs View output from containers
ps List containers
restart Restart services
rm Remove stopped containers
start Start services
stop Stop services
up Create and start containers
version Show the Docker-Compose version information
version: "3.7"
services:
vote:
build: .
command: /voter-app
environment:
VOTER_REDIS_ADDR: "voter-app_redis_1:6379"
VOTER_REDIS_PASSWORD: ""
VOTER_REDIS_DB: 0
ports:
- "8090:8080"
redis:
image: redis:alpine
ports: ["6379"]
volumes:
- voter_redis_data:/data
volumes:
voter_redis_data: {}
version: "3.7"
services:
prometheus:
image: prom/prometheus:v2.19.0
container_name: prom
ports:
- "9090:9090"
grafana:
image: grafana/grafana:7.0.3
container_name: grafana
ports:
- "3000:3000"
cd to docker_k8s_training/docker-compose/monitoring
docker-compose up
Load http://localhost:3000 and http://localhost:9090
cd to docker_k8s_training/docker-compose/consul_cluster
docker-compose up
Load http://localhost:8500
docker-compose up -d
docker-compose ps
docker-compose images
docker-compose stop
docker-compose rm
docker stop
docker rm
docker images
docker rmi
docker ps -a -f status=exited
docker rm $(docker ps -a -f status=exited -q)
Docker images consist of multiple layers. Dangling images are layers that have no relationship to any tagged images
They no longer serve a purpose and consume disk space
If you build an image without tagging it, the image will appear on the list of dangling images
docker images -f dangling=true
When you’re sure you want to delete them
docker image prune
To remove specific image
You would require to first stop and remove container using the image before doing this
docker rmi <image id/name:tag>
https://www.digitalocean.com/community/tutorials/how-to-remove-docker-images-containers-and-volumes
Docker commands
Building docker applications and run it
Using Docker compose