0% found this document useful (0 votes)
32 views10 pages

5docker Notes

This document provides instructions for various docker commands to create, run, manage, and inspect containers. Some key commands covered include: - docker run to create and run containers with options to override commands, publish ports, set environment variables, and mount volumes. - docker exec to run additional commands in existing containers without disturbing primary processes. - docker logs, docker stop, docker kill to view and manage container processes. - docker inspect to view detailed configuration and status of containers.

Uploaded by

Test
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views10 pages

5docker Notes

This document provides instructions for various docker commands to create, run, manage, and inspect containers. Some key commands covered include: - docker run to create and run containers with options to override commands, publish ports, set environment variables, and mount volumes. - docker exec to run additional commands in existing containers without disturbing primary processes. - docker logs, docker stop, docker kill to view and manage container processes. - docker inspect to view detailed configuration and status of containers.

Uploaded by

Test
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Create and Run in interactive docker run -it imageName docker run -it redis sh

mode with command( helpful for commandToRun


dry run and to validate what is
inside an image. Not used in
prod as this will override
command defined in the actual
image)
Create and Run with override Docker run image java -jar
command to run app.jar

Override entrypoint and


command

Docker run --entrypoint java


imageToUse -jar app.jar

Create but do not run. docker create imageName

this command will create a new


container but will not start it.
Execute an additional command docker exec -it <container id> docker exec -it
in an already existing container command myredisContainerID redis-cli
(without disturbing primary
command/process) exec – run another command docker exec -it
-i allow us to provide input to the myredisContainerID sh
Useful to debug live container container (Keep STDIN open)
in prod -t tty docker exec -it
myredisContainerID bin/sh

kubectl exec -it <POD_NAME> kubectl exec -it frontendpod


<COMMAND> bash

kubectl exec -it <POD_NAME> above command is deprecated.


-- <COMMAND> Use below instead ( just add --
infront of the command)

(OR)

kubectl exec -it frontendpod --


bash
Port forwarding from local port $ docker run -p $ docker run -p 8080:6379 redis
to container port portInLocalSystem:portInCon
tainer myImage
$ docker run -p 6379:6379 redis

Pass environment values using -e docker run -d docker run --env-file=env_file_name


flag to the container. -e alpine env
SSO_HOST_URL=http://someth
ing
-e SSO_PASS=A$B
my-image

if you declare environment


variable name but do not provide
value in run command then it
will be substituted from host
systems environment variable
value.

$docker run -d
-e SSO_HOST_URL
-e SSO_PASS
my-image

d run --env-file=my_env.properties -it


ubuntu bash

my_env.properties content
my_env_property=BLAH...

will be added to the environment

Run existing non running Docker start -a containerID


container
-a --> attached mode
See log Docker logs containerID
Follow the logs Docker logs -f containerId
--follow or -f

--since Docker logs --since 42m


Show logs since timestamp (e.g. containerid
2013-01-02T13:23:37Z) or
relative (e.g. 42m for 42
minutes)
$docker logs --tail 1000 -f
containerid
--tail , -nallNumber
of lines to
show from the end of the logs
Stop container gracefully Docker stop containerId If could not stop in 10 secs then
it will fallback to kill
command(refer next one)

--time ( OR -t) to change default 10 secs to


something else.

Stop container immediately Docker kill containerID


Restart container Docker restart containerId Seconds to wait for stop before
Restart one or more containers killing the container

--time ( OR -t) to change default 10 secs to


something else.

Show process that are running Docker top containerId


inside a container. Shows both
primary process (defined in the
Docker file and as well as
secondary process like sh in
docker exec -it containerId sh)
docker stats [OPTIONS]
[CONTAINER...]
Display a live stream of
container(s) resource usage $ docker stats
statistics

shows how much resource is


used by each containers like
cpu, memory, PIDS and network
etc

The PIDS column contains the


number of processes and kernel
threads created by that
container.

We can even create an image $ docker commit -c “CMD


[echo hi]”
from the running container runningContainerId
though it is not used in real
after -c we can provide
project as we should use override command to be
Dockerfile to make changes and used for new image. Here I
have added echo hi
recreate images and version the
changes.

But for quick testing or


validating purpose we can create
images from running container

Pause – we can pause running $ docker pause my_container


processes in side a container as
well.

The docker pause command


suspends all processes in the
specified containers. On Linux,
this uses the freezer cgroup.
Traditionally, when suspending a
process the SIGSTOP signal is
used, which is observable by the
process being suspended.

Probably useful when a long


running process executing and
wanted to make other system
changes in between so that
process can pick new changes
without killing and restarting
new container.

Unpause - to resume paused $ docker unpause my_container


processes in

Set conainer memory while docker run -m 512m nginx


starting a container
$ docker run -m 512m --memory-
-m or –-memory defines max reservation=256m nginx
memory limit

--memory-reservation defines min


memory

Defines CPU limit for a $ docker run --cpus=2 nginx


containers
$ docker run --cpus=2 --cpu-
--cpus = defines max cpu shares=2000 nginx

--cpu-shares = defines priority

Mounting a volume with docker run


-p3000:3000
container -v hostDir:/containerDir
-d
imageId

docker run
-p3000:3000
-v /containerDir/otherDir
-v hostDir:/containerDir
-d
imageId

when container Dir is


mapped to host system and
changes to the files in
host system will be
reflected in container as
well.

If containerDir not exists


then it will be created
inside the container

Note
/containerDir/otherDir
will not be mapped to host
system because that
volume does not have
mapping for it. Which
means that dir will be
untouched and have same
content as when the image
got created.

Stop running container $ docker stop


containerid(s)

$docker stop $(docker ps


-q) → stop all running
containers

Delete stopped containers $ docker rm containerId(s)

OR

$docker container prune →


to delete all the stopped
containers
Restart automatically Read in the blow sections docker run -dit --restart
[restart-policy-value]
[container_name]
Inspect container Docker inspect provides detailed $docker inspect containerId
information on constructs
OR
controlled by Docker.
$docker inspect 55c1595745fc
By default, docker inspect will
render results in a JSON array.
kubectl describe
For example uses of this resourceType
<resource_name>
command, refer to the examples
section below. kubectl describe pod
podName

Copy files and folders between Docker cp command is a docker cp from to


handy utility that allows
host and container
to copy files and folders
between a container and
the host system. Host to container

docker cp
<FILE_TO_COPY_FROM_HOST>
<CONTAINER_ID>:<PATH_INSID
E_CONTAINER_TO_PLACE_THE_F
ILE>

$ docker cp a.py
ubu_container:/home/dir1

Container to HOST

docker cp
<CONTAINER_ID>:<FILE_TO_CO
PY_INSIDE_CONTAINER>
<PATH__IN_HOST_TO_PLACE_FI
LE>

$ docker cp
ubu_container:/home/dir1/n
ew.txt /home/abhishek
Docker image commands

Pull image from $docker pull imageRepository:Tag $ docker pull redis:1.3.5


remote repo OR
$docker image pull imageRepository:tag

Push image from local $docker push userId/repositoryName:tag


to remote repository
userId → is user id of docker.io
repositoryName → image name
tag → version (latest, 1.0.1 etc)
Create a tag
TARGET_IMAGE $ docker image tag
that refers to SOURCE_IMAGE[:TAG]
SOURCE_IMAGE TARGET_IMAGE[:TAG]

Save an image as tar docker save [OPTIONS] IMAGE [IMAGE…] docker save myrepository:1.0.1 -o
myrepository.tar
file
Save one or more
images to a tar
archive

Load an image from $ docker load -i myrepository.tar


tar file
List all images $ docker images

$docker image ls
List changes to image $docker image history imageId
Delete images $docker rmi imageId
$docker rmi imageId imageId2

$docker image rm imageId


$docker image rm imageId imageId2
Delete unused images $docker image prune

Creating new image:


To create our own image we need to create a Dockerfile with below things
1. Specify a base image
2. Run some commands to install additional program and configuration to run application inside in
the image being created
3. Specify a command to run on container startup

Once Dockerfile is defined, we can execute below command to create new image out of it.
$ docker build -t dockerUserId/myImageName:v1 .

$ docker build -t sabtharshi/myImageName:v1 .


If your file name is not Dockerfile then you need to use -f flag to specify the file name
$ docker build -f Dockerfile.dev -t sabtharshi/myImageName:v1 .
Please note that the command ends with a dot (.) which is the build context path. Build context path is
nothing but where command is executing (directory path in local system). When needed, we can copy
files and folders from build context path to docker image that is being built.
Each steps in the Dockerfile, create a new temporary container based on previous step and applies the
new step inside the running container and then creates new images out of the temporary container. This
created image will be used to run the next step.
1. Below RED color highlighted line is the command fired to build new image from Dockerfile.
2. Yellow ones are the steps in the Dockerfile
3. Green ones are the temporary images created after executing each step
4. Purple ones are the temporary containers created using previous step image to add current step.
Once current step is added successfully, new image is created from this container and container will be
removed.
5. Green with yellow text is the final image

Dockerfile content Output in the terminal


FROM redis:latest root@sabthaa-
ubuntu:/home/sabthaa/Documents/docker_practic
RUN mkdir -p /home/redis-test e/redis-image# docker build -t mynewredis .
Sending build context to Docker daemon
2.048kB
RUN echo Step 1/4 : FROM redis:latest
"&&&&&&&&&&&&&&&&&&&&&&&&" ---> 7614ae9453d1
Step 2/4 : RUN mkdir -p /home/redis-test
CMD ["redis-server"] ---> Running in c72b6c917361
Removing intermediate container c72b6c917361
---> 56b93b67b590
Step 3/4 : RUN echo
"&&&&&&&&&&&&&&&&&&&&&&&
&"
---> Running in e04b41d78d40
&&&&&&&&&&&&&&&&&&&&&&&&
Removing intermediate container e04b41d78d40
---> 6b840afb1d69
Step 4/4 : CMD ["redis-server"]
---> Running in bbfb93eb1b00
Removing intermediate container bbfb93eb1b00
---> bd82b2f832fc
Successfully built bd82b2f832fc
Successfully tagged mynewredis:latest

when running RUN apt update always combine it with

Most useful commands in Dockerfile


FROM All docker file should drive from existing images. FROM ubuntu
Hence the first line should always be FROM.
Only one FROM can present in a Dockerfile.
FROM openjdk8:latest
Base image provide the OS that needed to run our
application.
MAINTAINER Who maintains the image
ENV To specify default environment variables. ENV name rishi
Available during both image build time and also
within the running container.

Can be overridden while starting new containers

$docker run -e some_variable=Me imageId


ARG ARG is only available during the build of a
Docker image (docker build etc), not after
the image is created and containers are
Syntax in Dockerfile
started from it (ENTRYPOINT, CMD). You
can use ARG values to set ENV values ARG
to work around that. some_variable_name=default_va
lue

This sets build time variale. Or without default value which


should be provided in build
command
ARG some_variable_name
$ docker build --build-arg
some_variable_name=a_val
ue -t myimage:v1 .
RUN To run some linux commands. Like updating apt RUN mkdir -p /home/redis
or installing new softwares/tools. Creating new RUN apt update
folders/files etc
WORKDIR Move to working directory inside WORKDIR /usr/app
image/container. If the path is not exists then it
will get created automatically.
COPY Copy files and folders from local system to COPY ./ ./opt
Docker image file system
./ is the location in local system
relative to build context
./opt is the location inside image

COPY ./ ./

In this example ./ is current


directory inside container

COPY ./package.json ./
ADD Similar to COPY If you want to add a xx.tar.gz to
a /usr/local in container, unzip it,
ADD provides additional features like remote and then remove the useless
URL and tar extraction support. compressed package.
For COPY:
COPY resources/jdk-7u79-linux-
x64.tar.gz /tmp/

RUN tar -zxvf /tmp/jdk-7u79-linux-


x64.tar.gz -C /usr/local

RUN rm /tmp/jdk-7u79-linux-
x64.tar.gz

For ADD:
ADD resources/jdk-7u79-linux-
x64.tar.gz /usr/local/

ADD supports local-only tar


extraction. Besides it, COPY will
use three layers, but ADD only
uses one layer.

You might also like