Skip to content

NOTE20181008

Somkiat Puisungnoen edited this page Jul 26, 2019 · 1 revision

Command for Docker Toolbox

$docker-machine ls
$docker-machine ip default

Command line with image

Pull image

$docker image pull :

List all images

$docker images
$docker image ls

See detail of image

$docker image inspect nginx:1.15.5-alpin

Command line with container

Create a container

$docker container run -d nginx:1.15.5-alpine

List all running containers

$docker container ls

List all containers

$docker container ls -a

Exceute to container (SSH)

$docker container exec -it <container id> <shell>
$docker container exec -it <container id> sh

Remove containers (Stop and Remove)

$docker container stop $(docker container ls -q)
$docker container prune


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

Create container with publish port

$docker container run -d -p 8081:80  nginx:1.15.5-alpine
$docker container run -d -p 8082:80  nginx:1.15.5-alpine
$docker container run -d -p 8083:80  nginx:1.15.5-alpine

Working with Images

  • Save image from container and push your image to registry (Docker Hub)
  • Create image from Dockerfile (basic)

Create new image from container

1. Create new container
$docker container run -d -p 8081:80  nginx:1.15.5-alpine

2. Execute to container and change data in file /usr/share/nginx/html/index.html
$docker container exec -it c0fb49c7b95c sh
#/exit

3. See diff in container
$docker container diff c0fb49c7b95c

4. Save container to new image
$docker container commit c0fb49c7b95c myweb:0.1

5. Try to create new container from new image
$docker container run -d -p 8082:80 myweb:0.1

Push new image to Registry (Docker Hub)

1. Login to Docker Hub
$docker login

2. Copy new image to correctly image name of Docker Hub
$docker image tag myweb:0.1 <your docker hub name>/myweb:0.1
$docker image tag myweb:0.1 somkiat/myweb:0.1

3. Push your image to Docker Hub
$docker image push somkiat/myweb:0.1

Create image from Dockerfile (basic)

1. Create Dockerfile
$touch Dockerfile

FROM nginx:1.15.5-alpine
WORKDIR /usr/share/nginx/html
COPY . /usr/share/nginx/html

2. Build image from Dockerfile
$docker image build -t thai:0.1 .

3. Create container from new image
$docker container -d thai:0.1

Working with container and volume

Using -v (volume) => format <host path>:<container path>
$docker container run -d -p 8081:80 -v $(pwd):/usr/share/nginx/html  nginx:1.15.5-alpine

For PWD (Play with docker)
1. Change own of folder and files to nginx
$chown -Rf nginx:nginx *

2. Create new container with volume
$docker container run -d -p 8081:80 -v $(pwd):/usr/share/nginx/html  nginx:1.15.5-alpine

Remove container when exited/stoped with --rm flag

$ docker container run -d --rm hello-world

$docker run -dit  --restart=always  thai:0.1

Access to container and stop nginx process and see detail of container
$docker container exec -it 7a943f95c249 sh
#/kill 1
#exit

$ docker container ps
Clone this wiki locally