0% found this document useful (0 votes)
41 views

4docker Notes

The document provides information about common Docker commands used to inspect containers, copy files between the host and container, manage Docker images, and create new Docker images using a Dockerfile. Some key commands discussed include docker inspect to view detailed information about a container, docker cp to copy files between the host and container, docker pull/push to download/upload images from/to a remote repository, docker build to create an image from a Dockerfile, and common instructions used in a Dockerfile like FROM, RUN, WORKDIR, and CMD.

Uploaded by

Test
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views

4docker Notes

The document provides information about common Docker commands used to inspect containers, copy files between the host and container, manage Docker images, and create new Docker images using a Dockerfile. Some key commands discussed include docker inspect to view detailed information about a container, docker cp to copy files between the host and container, docker pull/push to download/upload images from/to a remote repository, docker build to create an image from a Dockerfile, and common instructions used in a Dockerfile like FROM, RUN, WORKDIR, and CMD.

Uploaded by

Test
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

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