top of page
Writer's pictureNIKOLAS SCHAEFER

Dockerizing your Developer Workflow




What does "Dockerizing" mean?


Dockerizing means to place all local development and running databases/servers into docker containers. An example would be instead of downloading and install a database such as PostgreSQL you can just run it in a docker container with one command.



How it works


Docker for windows requires WSL(windows subsystem Linux) which is basically Linux in a virtual OS. On a codebase you can specify what environment you want your code to run in such as alpine Linux by using a Dockerfile. Docker simulates an OS by itself which ensures you don't have to install anything more than docker and that your code will always run itself.



Why Dockerize?


Dockerizing has significant benefits and is now often required by employers to do. Since developers can both run code in almost identical environments it prevents any setup causes and getting things to run on your machine. Since most production code will run on Linux servers anyway docker provides and easy way to test on them. Docker can also be used in production with Kubernetes to provide a seamless amount of servers and redirect server traffic.


How to Dockerize your environment


First off uninstall any databases you have currently such as PostgreSQL, Redis, MySQL or anything else. Then you want to install WSL by going to this link. Docker desktop is next which you can find here.


Now you have docker installed! run `docker` in the command line to see if it is setup properly. Now go to a project of your choice you wish to Dockerize. In this example I will be using a Golang project with a PostgreSQL database.



Dockerfile

This Dockerfile uses Multi-State Build to build the executable and then reduce the final image size. You can look more at Dockerfiles and how to build one to suit your environment by looking at my blog here or at the official docs.


My Dockerfile for a Golang environment looks like

FROM golang:1.16 AS build

WORKDIR /go/src/fiber

COPY . .

RUN go mod download

RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -a -installsuffix cgo -o app .

FROM alpine:latest

WORKDIR /app

COPY --from=build /go/src/fiber/app .

EXPOSE 3000

CMD ["./app"]

It runs on alpine Linux on the build and builds in a golang:1.16 version image. I can run `docker build -t myProject .` to build the image.


The PostgreSQL database also runs in a docker container but in a separate one. They can be connected in a docker network or a docker-compose.


Running the database command.

`docker run --name database -d -p 5432:5432 -e POSTGRES_PASSWORD=password postgres:alpine`

Then the connection string to this database will be

`host=localhost port=5432 user=postgres password=password dbname=postgres sslmode=disable`


Now we are able to get a production container for a project as well as run a database.


but what about developing in docker? Since this container runs in multi-stage builds we can only build the first stage.


Build in development stage

`docker build --target build -t base .`

Take note the target should be the dev stage which in this case is "build"


We can them bind the files to that container so we can change them locally with this command. This will also start the container

`docker run -p 3000:3000 --mount type=bind,source="{{current directory}}",target=/go/src/app --name {{container new name}} -td base`


This command exposes port 3000 on both the container and the local machine for actually testing the server.


Then you can rebuild the server with this command.

`docker exec -it web go run main.go`

This command rebuilds it but you can also change this command to incorporate hot reload or whatever you need. That's the beauty of docker and Dockerfiles.



And finally to kill the container and remove it you can run

`docker stop {image}; docker rm {image}`

23 views3 comments

Recent Posts

See All

3 comentários


KACY MORN
KACY MORN
26 de mai. de 2021

The amount of hours you must of put into learning all of this stuff must of been alot but it seems like you love what your doing.

Curtir

SOPHIA SCOTT
SOPHIA SCOTT
28 de abr. de 2021

This is very interesting and it looks like it could get confusing!

Curtir

mualsaeed
28 de abr. de 2021

Is their a way you can teach me to do DDOS Attacks? Great blog post btw!

Curtir
bottom of page