PharoView this PageEdit this PageUploads to this PageHistory of this PageHomeRecent ChangesSearch the SwikiHelp Guide)

Tutorial: Built a docker image for Pharo and Zinc

This is a short tutorial to show how to build a docker image with a Pharo 7 Smalltalk image running a simple Zinc Server. We then run the docker image as two running docker containers

STEP 1: Install docker on Ubuntu

 sudo apt-get update
 sudo apt-get install docker.io


STEP 2: Define and build a docker image


Usually we only need a file called Dockerfile. But as we want to use Pharo it makes sense
to write our Pharo code into a startup file we call load.st. Place both files in the same directory

Dockerfile

Create a file called Dockerfile with the following contents. Remember to use your name within the file:
######################################
# Based on Ubuntu image
######################################
FROM ubuntu

######################################
# Basic project infos
######################################
LABEL maintainer="Your Name"

######################################
# Update Ubuntu apt and install some tools
######################################
RUN  apt-get update \
  && apt-get install -y wget \
  && apt-get install -y git \
  && apt-get install -y unzip \
  && rm -rf /var/lib/apt/lists/*

######################################
# Have an own directory for the tool
######################################
RUN mkdir webapp
WORKDIR webapp

######################################
# Download Pharo using Zeroconf & start script
######################################
RUN wget -O- https://get.pharo.org/64/70+vm | bash

COPY load.st load.st

RUN ./pharo Pharo.image load.st

######################################
# Expose port 8080 of Zinc outside the container
######################################
EXPOSE 8080

######################################
# Finally run headless as server
######################################
CMD ./pharo --headless Pharo.image --no-quit


load.st file (place in same folder as dockerfile)

and here is the load.st file. For an easy example we just start the Zinc server on port 8080:

ZnServer startDefaultOn: 8080.
SmalltalkImage current snapshot: true andQuit: true


Build the docker image

Use a unique name for "yourcompany" (usually this is the name of your DockerHub account) and name for the docker image "firstimage"
Now we need to build the docker image using the definitions in our Dockerfile
 sudo docker build -t yourcompany/firstimage:v0.1 . 


Run it interactively as a container

In case we want to look at the results of the build we can run our docker image now. But by giving "/bin/bash"
as the last argument the docker image will start a bash (and not Pharo as defined in the last step using CMD in Dockerfile).
This way we first can have a look and control that anything is installed correctly:
 sudo docker run -it yourcompany/firstimage:v0.1 /bin/bash

You are now within a shell inside of the running new docker container (which is a runtime instance of a docker image).
Just type exit to leave the container after checking that anything is fine.

Run container as server

But now we really want to run the container. We map the exposed port 8080 from the container to our local port 8080:
sudo docker run --name sandbox1 -d -p:8080:8080 yourcompany/firstimage:v0.1

and run another container based on that docker image on another port
sudo docker run --name sandbox2 -d -p:8081:8080 yourcompany/firstimage:v0.1


Check that both are running

Now point your browser to http://localhost:8080 and http://localhost:8081 and you should be able to see the Zinc application from Pharo running

Show running Docker Container

If you want to check the running containers just use
sudo docker ps 


Stop it

To stop the container you can use:
sudo docker stop sandbox1
sudo docker stop sandbox2


Now you can go to DockerHub, register for an account and register/upload your image there. This is part of any DockerHub tutorial. So I leave this out here.

You can also use Container platforms like https://www.openshift.com (which provides a free version) to run the registered container in the cloud

-----------

Link to this Page