KNOW YOUR BUYER PERSONA

A buyer persona is a fictional representation of a person who is thought to be a typical consumer of a business. A buyer persona is a marketing phrase that refers to the traits of a certain kind of…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




Compose the containers

In this post I intend to introduce you to docker compose. Compose is a tool which lets you structure up your containers in a neat little yaml file.

Yaml is a “language” that is quite easy to read and quite easy to write, so I’m sure that it wont be any issues keeping up even without prior experience with it.

To start of, we should decide on a project to run in docker.
A good one to start with could be a very simple php page that just says “hello world” but runs through nginx, so that is where we start!

The images we will be using are

When we know what containers we want to run, we create our docker-compose file.
The file is (usually) named docker-compose.yml and the following is a “first step” version of the image, before we connect all services and add volumes and such.

Initially we tell docker-compose that it is a version 3 type of compose file.
We then define a set of services.

Our first service is called shared-data, it uses the image busybox which is a super tiny image, the container will be used only to hold data, nothing more. A data-only container is a container that is stopped as soon as it is created but due to it sharing data with other containers, it will be accessed (and in a sense started) on each access.

This container also has a restart option set to on-failure so that it restarts on crash.

Whats then is the difference between expose and port properties? you may ask. Well, expose opens up a port for its network-connected siblings, while ports opens up the ports for the host.
Exposing a port will not allow the host computer access to the container through that port, only the network connected containers.

If we start the docker containers we will have a few containers running, but they share no data nor any settings or configurations…
We need to fix this!

The files we want to serve will have to reside in the correct folders for docker-compose to add them to the data only container.
The container specifies ./src and ./nginx, which means that the directories it looks for are in the folder same folder as the compose file.
We might as well just create those two folders and then go on to adding files to them.

php file

Inside the src folder we create a index.php file with the following (this is not forced, you may do whatever you want!) code:

Save and close!

Now we have a php file, awesome!

That’s it! Now all the data that the shared-data container has is included at the same place in the fpm container!
We also have to do the same to the nginx server, because it also need to be able to access some files (both the php file and its settings that we will set up next).

nginx config file

I’m not going to go in to deep into the swamp of nginx.
I like nginx, but to go through all its configuration possibilities… well, that would turn my blog into a very nginx focused site!

The configuration we will use could look something like this:

In short, this configuration file will pass all calls to a php file to the index.php file served by the upstream, which is our fpm service.

Save the configuration file as nginx.conf and add it to the nginx directory.
Now what? well, we start the container of course!

When running up with docker-compose, we will start docker compose itself, it will look for a suitable docker-compose file and it will start all the services in it.
When we are done with the services we can close them down by docker-compose down and it will stop all the services until we next need them!
Now, if you run up with out any arguments, you are kind of stuck in the terminal with the output of the docker containers.
This could be a good thing when you run your containers, especially locally so that you can watch all the logs and output while it happens, but if you for some reason do not want it to be like that, just supply the -d argument to the command and it will be detached from all containers when they are started.

So:

Open browser and enter: localhost:8080 and you should be able to see the very beautiful site we just made available to your localhost!

Compose is production friendly and its possible to use overridable files and environment variables etc, I will post about this at a later time, but I thought it was worth noting!

Add a comment

Related posts:

Being a Tennessee Fan

Growing up in Knoxville, you don’t really have a choice. No, I’m not talking about whether or not you’re conservative or if you have an inexplicable affinity for Chick-Fil-A, but the fact that a…

The Adrian House

Paul sat in the food court outside the Los Angeles Ritz-Carlton looking for assassins. He scanned the rooftops for snipers. Was the man fumbling for his phone actually pulling a gun? Was the woman…

Buy Trustpilot Reviews

All you have to do is buy a trustpilot review for your business and we’re here to offer the best possible deal. Buying trustpilot reviews will be the best decision for any business owner as consumers…