-
Have a look at some important links
-
Clone this repository -
git clone https://github.com/akshaybharambe14/go-docker.git
-
Build, deploy and run a docker image -
To containerize our app, we need to build an "image". The "Dockerfile" instructs docker to build desired image. All the docker files can be found in "./docker" folder.
Explanation to docker commands is written in docker file itself.
Here, we are building 3 types of images
- Basic (size 814 MB)
- With docker volume (size 814 MB)
- Optimized multistage image with docker volume (Size 12 MB)
-
Publish your docker image to docker hub.
-
Create your account at docker
-
Log in to your account in docker cli with
docker login
-
Tag your image with format
<username>/<target-name>:<version>
docker tag go-docker akshaybharambe14/go-docker:v1.0.0
-
Push it to docker hub
docker push akshaybharambe14/go-docker:v1.0.0
Thats it. Now you have your own docker image.
-
-
-
Build a docker image - This will build with default "Dockerfile" at the root of repo.
docker build -t go-docker .
In our case, we need to provide path to Dockerfile with following command. This builds a docker image with name
go-docker
with file./docker/example.Dockerfile
docker build -t go-docker -f ./docker/example.Dockerfile .
-
List docker images -
docker image ls
You can see an image with name "go-docker" in the list.
-
Run the image in container -
docker run -d -p 6060:8080 go-docker
This will map 6060 port from host os to our app with port. We can access it with http://localhost:6060/
-
List running containers -
docker container ls
-
Stop the container with id
fff93d13a484
docker container stop fff93d13a484
-
-
As we have a logger in our app, which logs to a file named
app.log
. This file is not visible to use. For this we need to redirect our logs to a location on host machine.-
Build -
docker build -t go-docker-volume -f ./docker/volume.Dockerfile .
-
Run - This will map
D:/logs
from your host machine to/app/logs
on the container. You can see the logs atD:/logs/app.log
. The log file name is provided in the Dockerfile.docker run -d -p 6060:8080 -v D:/logs:/app/logs go-docker-volume
-
-
-
Build -
docker build -t go-docker-optimized -f ./docker/multistage.Dockerfile .
-
Run -
docker run -d -p 6060:8080 -v D:/logs:/app/logs go-docker-optimized
-