Docker Guide

What is docker?

Getting Started

Download docker and install it on your computer

  • Link to developer guide – https://docs.docker.com/ee/
  • Select the docker option based on your operation system. In my case, I selected Docker for desktop mac. Then from there should be a link to download the docker executable onto your computer.
  • You will have to create a docker account to download docker.
  • Install docker by double-clicking on the executable your downloaded and follow instructions.

Docker commands

You are going to use the command-line interface to work with Docker.

docker images
– list out all the images in your computer

docker ps -a
– list out all the running containers on your computer

docker rmi imageId
– delete or remove an image.

docker stop containerId
– stop a running container

docker rm containerId
– delete a container

docker pull image: tag
– pull an image from a docker registry(docker hub or AWS ECR).

docker pull mysql

 

Run docker container automatically

Use these tags 

-dit --restart unless-stopped

docker run -p outside-port:internal-port –name container-name -dit –restart unless-stopped -d image_name:image_tag
– run docker container with the port where the container will use both externally and internally, the name of the container, the image, and tag being used, and a command to make sure the container will restart if it stops.

– -d means that to run container in background and print container ID

docker run -p outside-port:internal-port --name container-name -dit --restart unless-stopped -d image_name:image_tag
docker run -p 8080:8080 --name springboot-hello-0-0-1 -dit --restart unless-stopped -d springboot-hello:0-0-1

 

Push up a docker image to aws docker registry

1. Login to aws ECR from your computer

aws ecr get-login-password --profile folauk110

2. Build image

docker build -t project .

3. Tag image

docker tag project:latest remote-ecr-url/project:latest

4. Push image to remote registry

docker push remote-ecr-url/project:latest

 

SSH into a docker container

docker exec -it {container-name} sh

or

docker exec -it {container-name} sh /bin/bash

 

Install Redis using docker

Pull redis docker image

docker pull redis

Run redis docker container

docker run --name container-redis -dit --restart unless-stopped -p 6379:6379 -d redis

Set keys and values in redis

First, ssh into the redis container.

docker exec -it container-redis sh

Second, use the redis-cli

redis-cli
127.0.0.1:6379> set docker great
OK
127.0.0.1:6379> get docker
"great"
127.0.0.1:6379> set name objectrocket
OK
127.0.0.1:6379> get name
"objectrocket"

 

Install Elasticsearch using docker

Pull elasticsearch docker image

Make sure to use the right version that your code and Kibana is using.

docker pull docker.elastic.co/elasticsearch/elasticsearch:7.1.0

Run elasticsearch docker container

docker run -p 9200:9200 -p 9300:9300 --name elasticsearch -e "discovery.type=single-node" -dit --restart unless-stopped -d docker.elastic.co/elasticsearch/elasticsearch:7.1.0
Add Elasticsearch Password
-e ELASTIC_PASSWORD=MagicWord
docker run -p 9200:9200 -p 9300:9300 --name elasticsearch -e "discovery.type=single-node" -e ELASTIC_PASSWORD=DDloadjes233j -dit --restart unless-stopped -d docker.elastic.co/elasticsearch/elasticsearch:7.1.0

 

Install Kibana using docker

Pull kibana docker image

Make sure to use the right version that your code and elasticsearch is using.

docker pull docker.elastic.co/kibana/kibana:7.1.0

Run kibana docker container 

docker run --link elasticsearch:elasticsearch -dit --restart unless-stopped --name kibana -p 5601:5601 docker.elastic.co/kibana/kibana:7.1.0

Access kibana locally by going on http://localhost:5601/app/kibana

Add Elasticsearch Password

# add elasticsearch password
-e ELASTICSEARCH_PASSWORD=DDloadjes233j

Example
docker run --link elasticsearch:elasticsearch -e ELASTICSEARCH_PASSWORD=DDloadjes233j -dit --restart unless-stopped --name kibana -p 5601:5601 docker.elastic.co/kibana/kibana:7.1.0

Logstash

# pull image
docker pull docker.elastic.co/logstash/logstash:7.15.0

# run container
docker run -dit --restart unless-stopped --name logstash -v ~/pipeline/:/usr/share/logstash/pipeline/ docker.elastic.co/logstash/logstash:7.15.0

 

docker pull docker.elastic.co/logstash/logstash:7.15.0

 

Postgresql with docker

#pull image
docker pull postgres:12.6
#run docker container
docker run --name postgres --restart unless-stopped -p 5432:5432 -e POSTGRES_PASSWORD=test -v /Users/folaukaveinga/Software/postgres/data:/var/lib/postgresql/data -d postgres:12.6

 

Ubuntu with docker

Sometimes when you want to play or learn linux you want to have a linux OS to play with. With docker you can pull a ubuntu docker image and run it locally.

docker pull ubuntu

Run a ubuntu container

docker run -it ubuntu /bin/bash

You will be in the root folder of the ubuntu container right away after running the above command.

Restart a docker container

docker container restart [OPTIONS] CONTAINER [CONTAINER...]

// -t or --time represents seconds to wait for stop before killing the container
docker container restart -t 5 bee0a80b1424

 

View logs of a docker container

docker logs [OPTIONS] CONTAINER

// Follow log output -f or --follow
docker logs -f asdfwers

 

 




Subscribe To Our Newsletter
You will receive our latest post and tutorial.
Thank you for subscribing!

required
required


Leave a Reply

Your email address will not be published.