Running Pharo 9 in Docker

Docker is an excellent tool to manage containers, and execute applications on them. This is not a discovery!! The idea of this post is to show how easy and simple is to have a Pharo 9 Seaside application running in Docker.

Initial Considerations

This post is based in the excellent Docker image written by the Buenos Aires Smalltalk group (https://github.com/ba-st). They maintain a repository with configurations for different Docker images from Pharo 6.1 to Pharo 9.

You can check the repository in https://github.com/ba-st/docker-pharo

These images are also available from Dockerhub (https://hub.docker.com/r/basmalltalk/pharo), so you can choose also to download the images ready from there. As we are going to do in this small example.

Also, to complete this example we are using an existing Seaside application. We are using the example of TinyBlog. This is an excellent tool to learn Seaside, Voyage and Pharo in general. It is available here.

We are using the latest stage of the project, that is hosted in https://github.com/LucFabresse/TinyBlog

Creating a Docker Image for our Application

In order to start a container with our application, we need to create an image with all the requirements installed and built. Once we have it, it is possible to start one or more instances of this application.

For doing so, we are going to start from the image from basmalltalk/pharo:9.0-image.

We need to pull this image from Dockerhub so it is available for us to use, we execute so:

docker pull basmalltalk/pharo:9.0-image

Once we have the initial image, we need to give a Dockerfile with the recipe to build our application image. The downloaded image already come with a Pharo9 VM and image. We need to perform the following steps on this image:

  • Install our application with all the dependencies using Metacello
  • Generate the initial test data of the application
  • Define an entry point that will execute the Zinc server
  • Expose the Zinc server port so it can be used outside the container

For doing so, we are going to create a file called Dockerfile with the following content:

FROM basmalltalk/pharo:9.0-image
RUN ./pharo Pharo.image eval --save "Metacello new \
baseline:'TinyBlog'; \
repository: 'github://LucFabresse/TinyBlog/src'; \
onConflict: [ :ex | ex useLoaded ]; \
load"
RUN ./pharo Pharo.image eval --save "TBBlog reset ; createDemoPosts"
EXPOSE 8080/tcp
CMD ./pharo Pharo.image eval --no-quit "ZnZincServerAdaptor startOn: 8080"

Once we have a Dockerfile stored, you can put wherever you like it. It is time to build an image using it. We need to be in the directory next to the Dockerfile and execute:

docker build -t pharo/tinyblog:latest .

This will create a Docker Image using the description of the Dockerfile in the current directory, and the new image will be called pharo/tinyblog with a tag marking it as latest.

Once the process is finished, if we list the images with

docker images

We get:

REPOSITORY     TAG    IMAGE ID     CREATED        SIZE
pharo/tinyblog latest fee45c26e604 56 minutes ago 727MB

Executing Our Application

Once we have an image of our application, it is possible to execute this image as one or more containers. We are going to execute a container with the image, and we are going to redirect the port 8080 to the outside; so we can access it.

For doing so, we execute:

docker run -d -P pharo/tinyblog

This will execute our image pharo/tinyblog in detached mode (-d), so it will run in the background, and publishing all ports to the outside (-P). The command will return the ID of the container.

This is a really simple example of running an application, as this is not a Docker tutorial we are only to show a little simple example.

If we check the running containers with:

docker ps

We can see the information about the running containers

CONTAINER ID IMAGE          COMMAND                CREATED STATUS            PORTS                   NAMES
3191540dbeb3 pharo/tinyblog "/bin/sh -c './pharo…" 44 minutes ago Up 44 minutes 0.0.0.0:32768->8080/tcp fervent_goldstine

We can see that our application is running , and that the redirected por is 32768. Also, we can see some statistics about the image and the ID and a fantasy name, we can use any of them to refer it in any docker command like stop, rm, etc.

If we access with our browser to the url http://localhost:32768/TinyBlog we can see the running application.

Once, we have our container we can do any other thing that we can with containers. From stopping it, resuming it, using it in collaboration with other containers or in a multi container infrastructure. But that…. is for other story.

Conclusion

This small post is just to show how the different tools and technologies of Pharo can easily be integrated with state-of-the-art solutions. If it is interest to the community, this post can be the start of nice infrastructure serie.

Published by Pablo Tesone

I am one of the Pharo Consortium Engineers. I am an enthusiast object-oriented programming and environment!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: