100% found this document useful (1 vote)
43 views7 pages

04-Jun-2021 Docker

This document provides commands for working with Docker images and containers. Some key commands include: 1. docker pull to download an image 2. docker run to create and start a container from an image with options like -d for detached mode, -p for port mapping, and -it for an interactive terminal 3. docker ps to view running containers and docker container ls to view all containers 4. docker stop, docker rm to stop and remove containers The document then provides examples of running common images like Tomcat, Jenkins, Nginx, MySQL and CentOS as containers with different configurations.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
43 views7 pages

04-Jun-2021 Docker

This document provides commands for working with Docker images and containers. Some key commands include: 1. docker pull to download an image 2. docker run to create and start a container from an image with options like -d for detached mode, -p for port mapping, and -it for an interactive terminal 3. docker ps to view running containers and docker container ls to view all containers 4. docker stop, docker rm to stop and remove containers The document then provides examples of running common images like Tomcat, Jenkins, Nginx, MySQL and CentOS as containers with different configurations.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 7

Docker Commands

--------------------
Working on Images
-------------------------
1 To download a docker image
docker pull image_name

2 To see the list of docker images


docker image ls
(or)
docker images

3 To delete a docker image from docker host


docker rmi image_name/image_id

4) To upload a docker image into docker hub


docker push image_name

5) To tag an image
docker tag image_name ipaddress_of_local_registry:5000/image_name

6) To build an image from a customised container


docker commit container_name/container_id new_image_name

7) To create an image from docker file


docker build -t new_image_name

8) To search for a docker image


docker search image_name

9) To delete all images that are not attached to containers


docker system prune -a

++++++++++++++++++++++++++++++++++++++++++++++

Working on containers
-----------------------------

10) To see the list of all running continers


docker container ls

11) To see the list of running and stopped containers


docker ps -a

12) To start a container


docker start container_name/container_id
13) To stop a running container
docker stop container_name/container_id

14) To restart a running container


docker restart container_name/container_id
To restart after 10 seconds
docker restart -t 10 container_name/container_id

15) To delete a stopped container


docker rm container_name/container_id

16) To delete a running container


docker rm -f container_name/container id

17) To stop all running containers


docker stop $(docker ps -aq)

18) To restart all containers


docker restart $(docker ps -aq)

19) To remove all stopped containers


docker rm $(docker ps -aq)

20) To remove all contianers(running and stopped)


docker rm -f $(docker ps -aq)

21) To see the logs generated by a container


docker logs container_name/container_id

22) To see the ports used by a container


docker port container_name/container_id

23) To get detailed info about a container


docker inspect container_name/container_id

24) To go into the shell of a running contianer which is moved into background
docker attach container_name/container id

25) To execute anycommand in a container


docker exec -it container_name/container_id command
Eg: To launch the bash shell in a contianer
docker exec -it container_name/container_id bash

26) To create a container from a docker image ( imp )


docker run image_name

++++++++++++++++++++++++++++++++++++++++++++++++

Run command options

-it for opening an interactive terminal in a container

--name Used for giving a name to a container

-d Used for running the container in detached mode as a background process

-e Used for passing environment varaibles to the container

-p Used for port mapping between port of container with the dockerhost port.

-P Used for automatic port mapping ie, it will map the internal port of the
container
with some port on host machine.
This host port will be some number greater than 30000

-v Used for attaching a volume to the container

--volume-from Used for sharing volume between containers

--network Used to run the contianer on a specific network


--link Used for linking the container for creating a multi container
architecture

--memory Used to specify the maximum amount of ram that the container can use

++++++++++++++++++++++++++++++++++++++++++++++++++=

# docker images ( There are no images )

To download tomcat image

# docker pull tomee

# docker images

# docker pull ubuntu


If you do not specify the version, by default, we get latest version

I want to download jenkins


# docker pull jenkins

TO create a container from an image

# docker run --name mytomcat -p 7070:8080 tomee

docker run --name c1 -p 7070:8080 tomee

TO check the tomcat is running or not


http://13.250.47.90:7070

( 7070 is port number mapped in docker host)

Lets remove the container ( Open another gitbash terminal)

# docker stop c1

# docker rm -f c1

# docker run --name mytomcat -p 7070:8080 -d tomee

( The above command runs tomcat in detached mode , so we get out # prompt back )

# docker container ls

TO start jenkins
# docker run --name myjenkins -p 9090:8080 -d jenkins

To check for jenkins ( Open browser )


http://13.250.47.90:9090

To create ubuntu container


# docker run --name myubuntu -it ubuntu

Observation: You have automatically entered into ubuntu


# ls ( To see the list of files in ubuntu )
# exit ( To comeout of container back to host )

+++++++++++++

Scenario 1:
Start tomcat as a container and name it as "webserver". Perform port mapping and
run this container in detached mode

# docker run --name webserver -p 7070:8080 -d tomee

To access homepage of the tomcat container


Launch any browser
public_ip_of_dockerhost:7070

++++++++++++++++++++++++++++++++

Scenario 2:
Start jenkins as a container in detached mode , name is as "devserver", perform
port mapping

# docker run -d --name devserver -p 9090:8080 jenkins

To access home page of jenkins ( In browser)


public_ip_of_dockerhost:9090

++++++++++++++++++++++++++++++++++++++++

Scenario 3: Start nginx as a container and name as "appserver", run this in


detached mode , perform automatic port mapping

Generally we pull the image and run the image

Instead of pulling, i directly

# docker run --name appserver -P -d nginx

( if image is not available, it perform pull operation automatically )


( Capital P , will perform automatic port mapping )

How to check nginx is running or not? ( we do not know the port number)

To know the port that is reserved for nginx )


# docker port appserver
80/tcp -> 0.0.0.0:32768

80 is nginx port
32768 is dockerhost port
or

# docker container ls ( to see the port of nginz and docker host )

To check nginx on browser


52.221.192.237:32768

++++++++++++++++++++++++++
To start centos as container

# docker run --name mycentos -it centos


# exit ( To come back to dockerhost )

++++++++++++++

Scenario 3: Start nginx as a container and name as "appserver", run this in


detached mode , perform automatic port mapping

Generally we pull the image and run the image

Instead of pulling, i directly

# docker run --name appserver -P -d nginx

( if image is not available, it perform pull operation automatically )


( Capital P , will perform automatic port mapping )

How to check nginx is running or not? ( we do not know the port number)

To know the port that is reserved for nginx )


# docker port appserver
80/tcp -> 0.0.0.0:32768

80 is nginx port
32768 is dockerhost port

or

# docker container ls ( to see the port of nginz and docker host )

To check nginx on browser


52.221.192.237:32768

++++++++++++++++++++++++++
To start centos as container

# docker run --name mycentos -it centos


# exit ( To come back to dockerhost )

++++++++++++++

To start mysql as container, open interactive terminal in it, create a sample


table.
# docker run --name mydb -d -e MYSQL_ROOT_PASSWORD=sunil mysql:5

# docker container ls

I want to open bash terminal of mysql


# docker exec -it mydb bash

To connect to mysql database


# mysql -u root -p

enter the password, we get mysql prompt

TO see list of databases


> show databases;

TO switch to a databse
> use db_name
> use mysql

TO create emp tables and dept tables

https://justinsomnia.org/2009/04/the-emp-and-dept-tables-for-mysql/

> exit
# exit
# exit

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy